A file is collection of related records, a record is composed of several fields and field is a group of character.
This requires another standard C++ library called fstream which defines three new data types:
This data type represents the output file stream and is used to create files and to write information to files.
This data type represents the input file stream and is used to read information from files.
This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.
1.Open an existing file
2.Read from file
3.Write to a file
4.Moving a specific location in a file(Seeking)
Opening a File
A file must be opened before you can read from it or write to it. Either the ofstream or fstream object may be used to open a file for writing and ifstream object is used to open a file for reading purpose only.Following is the standard syntax for open() function, which is a member of fstream, ifstream, and ofstream objects.
void open(const char *filename, ios::openmode mode);
Here, the first argument specifies the name and location of the file to be opened and the second argument of the open() member function defines the mode in which the file should be opened.
file open modes
ios::app – Append mode. All output to that file to be appended to the end.
ios::ate – Open a file for output and move the read/write control to the end of the file.
ios::in – Open a file for reading.
ios::out – Open a file for writing.
ios::trunk – If the file already exists, its contents will be truncated before opening the file.
Closing a File
When a C++ program terminates, it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.
Writing to a File
While doing C++ programming, you write information to a file from your program using the stream insertion operator (<<) just as you use that operator to output information to the screen. The only difference is that you use an ofstream or fstream object instead of the cout object.
Reading from a File
You read information from a file into your program using the stream extraction operator (<<) just as you use that operator to input information from the keyboard. The only difference is that you use an ifstream or fstream object instead of the cin object.
File Position Pointers
Both istream and ostream provide member functions for repositioning the file-position pointer. These member functions are seekg (“seek get”) for istream and seekp (“seek put”) for ofstream.The argument to seekg and seekp normally is a long integer. A second argument can be specified to indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to the beginning of a stream, ios::cur for positioning relative to the current position in a stream or ios::end for positioning relative to the end of a stream.