Before getting into this section, it is recommended that you have a proper understanding of Text file and Binary File. If any of both seems strange to you we give short information about these files:- Text file. The text file stores the data in ASCII character. Each line of text is terminated with a special character known as EOL (End of Line) character or delimiter character in text files. When this EOL character is read or written, certain internal translations take place. Binary file. The Binary File stores the data in the same format as it is held in memory. No delimiters are used for a line and no translations occur here in binary files. The following stream classes can be used in C++ to working with files: ofstream: Write on files ifstream: Read from files fstream: Both read and write from/to files.
To open a file we use open() command as following: ofstream cppfile; cppfile.open(“filename”,mode); In the above line: ofstream cppfile:
By using ofstream we declared a file variable cppfile who used to access file. cppfile.open (“filename”,mode): You can see that we use the file variablecppfile with open() to open a file. filename is a file which to be open and modecan be the following : File mode
Append to end of file
go to end of file on opening
file open in binary mode
open file for reading only
open file for writing only
open fails if the file does not exist
open fails if the file already exist
delete the contents of the file if it exist
When we use two mode at a time we have to use operator OR (|) between mode. For example, if we want to open the file example.dat in binary mode to add data we could do it by the following: fstream myfile;
myfile.open (“example.dat”, ios::out | ios::app | ios::binary);
If you have opened any file then you must close the opened file by following command: myfile.close();
INPUT AND OUTPUT OPERATION
put() Function :
The function put() writes a single character to the associated stream.
get() Function :
Similarly, the function get() reads a single character form the associated stream. example :
write() function :
write() function write blocks of binary data.
read() Function :
read() function read blocks of binary data.
myfile.write((char *)&obj, sizeof(obj));
myfile.read((char *)&obj, sizeof(obj));
ERROR HANDLING FUNCTION
RETURN VALUE AND MEANING
Returns true if a reading or writing operation fails,
Returns true if a file open for reading has reached the end. bad()
Returns true if an invalid operation are attempted, for example if you are trying to write to a file that is not opened for writing operation or that file has no space left. good()
Returns true if no error has occurred.
All Input Output streams objects have at least one internal stream pointer: ifstream is a just like istream, and it has a get pointer that points to the element to be read in the next input operation. ofstream is a just like ostream, and it has a put pointer that points to the location where the next element has to be written. fstream, inherits the get and the put pointers, from iostream.
The above internal stream pointers can be manipulated using the following member functions:
moves get pointer(input) to a specified location
moves put pointer (output) to a specified location
gives the current position of the get pointer
gives the current position of the put pointer
The other prototype for above functions is:
seekg(offset, ref_position );
seekp(offset, ref_position );
The parameter offset represents the number of bytes the file pointer is to be moved from the location specified by the parameter ref_position. Theref_position takes one of the following three constants defined in the ios class.
ios::beg start of the file
ios::cur current position of the pointer
ios::end end of the file
Data Handling with Text file in C++
Example to write in a text file