Ad Code

Responsive Advertisement

C++ file handling - learn CPP

C++ file handling - learn CPP, File handling or file handling in programming languages ​​means the process of performing a specific operation on the files on the computer, such as: reading the content of a file, displaying it in the program, creating a copy of it, modifying its content or deleting it, whether the file type is (jpg). ), (mp4) or any other type, and in the programming language (C++) files are handled by using three classes, which are (fstream), (ifstream), (ofstream) available in (fstream headerfile), and to deal with files You must include package() because it contains the classes assigned to it, in addition to package().


C++ file handling - learn CPP


Classes of the fstream package in the C++ programming language


package() contains the following set of basic classes that can be used to work with files:


class ofstream


Used to create an object that allows the user to create and write to a new file.


class ifstream


Used to create an object that allows the user to read the content of the file.


class fstream


Used to create an object that allows us to create a new file, write to it, and read from it. As this class is considered a combination of the ofstream class and the ifstream class.


How to open and close a file in C++


If the user wants to read or write the content of a file, this file must be opened by your program itself in order to be able to do so, and the three classes (ifstream), (ofstream) and (fstream) all contain a function called open Use it to open the file we want to deal with.


And upon completion of dealing with any file written in the programming language (C++), the user must close it directly, because this will improve the performance of the program as it will reduce the size of the space reserved for the file in memory, in addition to that you will be able to deal with this The file is directly outside your program, and the three classes (ifstream), (ofstream) and (fstream) all contain a function called (()close) that we use to close the file.


There are four ready-made functions that the user can use to ensure that the connection to the file is correct and that no problem occurred when dealing with it, whether when reading or writing in it:


function bool bad()


It is used to find out if there is any problem when reading or writing the file. Returns true if there is a problem and false if no problem occurs.


function bool fail()


Exactly the same as the previous function, in addition to helping to identify problems that may occur when dealing with the file content.


function bool eof()


It is an abbreviation of End Of File and it is used to find out if reading or writing reached the end of the file or not. It returns true if the translator reached the end of the file and false if it reaches its end.


function bool good()


It is used to find out if there was any problem when dealing with the file, and it includes all kinds of problems that may occur to files.