Introduction
In this lab, you will learn how to read and write a file line by line using C++ programming language. You will create a file, write to it, and then read its content one line at a time.
In this lab, you will learn how to read and write a file line by line using C++ programming language. You will create a file, write to it, and then read its content one line at a time.
Create a C++ file named file_io.cpp
in the ~/project
directory. In this step, you will create and open a file named example.txt
.
#include <fstream>
int main() {
// Creating and opening a file
std::ofstream ofs;
ofs.open("example.txt", std::ofstream::out | std::ofstream::trunc);
ofs.close();
return 0;
}
Explanation:
ofstream
is a class for writing to files.ofs
is an object of ofstream
class.open()
method is used to create and open a file.open()
method, the first argument is the name of the file to be created and the second argument specifies the mode in which the file should be opened. Here, out
specifies that the file is opened for writing, and trunc
specifies that the file should be truncated if it already exists.In this step, you will write to the file example.txt
.
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ofstream ofs;
ofs.open("example.txt", std::ofstream::out | std::ofstream::trunc);
if (ofs.is_open()) {
std::cout << "Enter text to write to the file (Enter empty line to exit):" << std::endl;
std::string line;
do {
std::getline(std::cin, line);
if (line.length()) {
ofs << line << std::endl;
}
} while (line.length());
ofs.close();
} else {
std::cerr << "Error in opening the file" << std::endl;
}
return 0;
}
Explanation:
std::cout
is used to display a message on the console asking the user to enter text to write to the file.std::getline()
is used to read the user input line by line and store it in the line
variable.if (line.length())
is used to check whether the user has entered any text or not. If the user has not entered any text, then the loop will exit.ofs << line << std::endl;
is used to write the user input to the file followed by a newline character.std::cerr
is used to display an error message on the console if the file cannot be opened.To run this program, go to the ~/project
directory and run the following command:
g++ file_io.cpp -o file_io && ./file_io
This program will prompt the user to enter text to write to the file, and then write the user input to the file.
In this step, you will read the contents of the example.txt
file.
#include <fstream>
#include <iostream>
#include <string>
int main() {
std::ifstream ifs;
ifs.open("example.txt");
if (ifs.is_open()) {
std::cout << "Contents of the file:" << std::endl;
std::string line;
while (std::getline(ifs, line)) {
std::cout << line << std::endl;
}
ifs.close();
} else {
std::cerr << "Error in opening the file" << std::endl;
}
return 0;
}
Explanation:
std::ifstream
is a class for reading from files.ifs
is an object of ifstream
class.open()
method is used to open an existing file.open()
method, the argument is the name of the file to be opened.std::cout
is used to display a message on the console to indicate that the contents of the file are being displayed.std::getline()
is used to read the contents of the file line by line and store it in the line
variable.std::cout << line << std::endl;
is used to display the contents of the file line by line on the console.To run this program, go to the ~/project
directory and run the following command:
g++ file_io.cpp -o file_io && ./file_io
This program will display the contents of the example.txt
file on the console.
In this lab, you learned how to create, write to, and read from a file using C++ programming language. You learned how to use ofstream
and ifstream
classes to write to and read from files, respectively. You also learned how to use open()
method to create and open a file, and getline()
method to read the contents of a file line by line.