Read C++ files not difficult. Here are the details Things you need to know about reading and writing C++ files.
What is file handling in C++?
The basic object that stores user related data is called a file. Files can be of many types described by their extension. For example: .txt (text file), .cpp (C++ source file), .exe (executable file), .pdf (portable document file), and many others.
File processing is the name of the file manipulation task that stores related data in a programming language, specifically C++. This allows you to store data in a permanent archive, even after the program executes file processing for the same end of its execution. C++ provides the fstream library for file processing.
In this article, we will discuss classes that can implement I/O operations on files. To take input from keyboard and print data on console. Maybe you are using cin (character input stream) and cout (character output stream) of istream and ostream classes. File streams are similar, except that the console is replaced by a file.
What is fstrem library?
The fstream library provides C++ programmers with 3 classes that work with files. These classes include:
- Ofstream: Represents the output stream. It is used for creating and writing information to files.
- Ifstream: Represents the input stream, used to read information from the data file.
- Fstream: Represents a stream file, including ofstream/ifstream features. Thus, it is capable of creating, writing, and reading data in files.
To perform file processing in C++, you include header files as
Open a File in C++
A file must be open before you can read information from it or write information to it. Or object ofstream or object fstream can be used to open a file for writing purposes or the ifstream object is used to open a file for reading purposes only.
Below is the standard syntax for the open() function, which is a member of the fstream, ifstream, and ofstream objects in C++:
void open(const char *ten_file, ios::che_do);
Here, the first parameter specifies the name and location of the file to be opened and the second parameter of the member function open() Defines the mode in which the file should be opened.
Regime | Describe |
---|---|
ios::app | Append mode. All output to that file is appended to the end of that file |
ios::ate | Open a file for outpur and move read/write control to the end of the file |
ios::in | Open a file to read |
ios::out | Open a file for recording |
ios::trunc | If this file already exists, its content will be truncated before opening the file |
You can combine two or more of these values by editing them together (using (|). For example, if you want to open a file in write mode and want to truncate it in case it has been exists, you follow the following syntax:
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
In the same way, you can open a file for reading and writing purposes as follows:
fstream QTM;
QTM.open("file.dat", ios::out | ios::in );
Close a File in C++
When a C++ program ends, it automatically closes all Streams, releases all allocated memory, and closes all opened files. But it is good practice for a programmer to close all opened files before ending the program.
Below is the general syntax for the function close() in C++, is a member of the C++ fstream, ifstream, and ofstream objects:
void close();
Write File in C++
While programming in C++, you write information to a file from your program using the stream insertion operator (ofstream or fstream in C++ instead of object cout in C++.
Reading a File in C++
You read information from a file in your C++ program using the stream extraction operator (>>), just as you use it to enter keyboard input. The difference is that you use an ifstream or fstream object instead of using the cin object in C++.
Example of Reading and Writing files in C++
The following C++ program opens a file in read and write mode. After writing the information entered by the user to a file called qtm.dat, the program reads the information from that file and produces the output on the screen:
#include
#include
using namespace std;
int main ()
{
char data[100]; // mo mot file trong che do write.
ofstream outfile;
outfile.open("qtm.dat");
cout > data;
cin.ignore();
// ghi du lieu da nhap vao trong file.
outfile > data;
// ghi du lieu tren man hinh.
cout > data; cout
Compiling and running the above C++ program will produce the following results:
The above example uses additional functions from the cin object, such as the getline() function to read the line from the outside and the ignore() function to ignore extra characters to the left of the previous read command.
File location pointer in C++
Both istream and ostream objects provide member functions for repositioning the file-position pointer. These member functions are seekg (short for seek get) for istream and seekp (short for seek put) for ostream in C++.
Parameter for seekg and seekp love is one long int. A second parameter can be specified to indicate the direction of the search. The search direction can be ios::beg (default) to determine the position relative to the start of a Streamis ios::cur to determine the position relative to the current position in one Stream or ios::end to specify the position relative to the end of a Stream in C++.
The file location pointer is an integer value that identifies a position in a file, calculated as the number of bytes from the starting position of that file. Here are some examples to determine the position of the file location pointer in C++:
// xac dinh vi tri byte thu n cua doi tuong file
doi_tuong_file.seekg( n );
// xac dinh vi tri n byte ve sau cua doi tuong file
doi_tuong_file.seekg( n, ios::cur );
// xac dinh vi tri n byte bat dau tu cuoi cua doi tuong file
doi_tuong_file.seekg( n, ios::end );
// xac dinh vi tri tai cuoi doi tuong file
doi_tuong_file.seekg( 0, ios::end );
According to Tutorialspoint
Previous article: Interface in C++ (Abstract class)
Next article: Exception Handling in C++