Most computer programs work with files because it helps stores the information permanently. The information and data stored under a special name on a storage device are called a file. Stream refers to a sequence of bytes. A file itself is the bunch of bytes stored on storage devices like tape, magnetic disk, etc. The binary file is a file that contains information in the same format as it is stored in memory. In binary files are used for a line, and no translations occur here.
C++ File Handling
To handle files in C++, there are mainly dealt with using the three classes fstream, ifstream, and ofstream available in the fstream header file.
- ofstream: Stream class to write on the files
- ifstream: Stream class to read from the files
- fstream: This Stream class to read and write from the /to files.
Operation performed on the file
- Creation of the new file
- Opening an existing file
- Reading from the file
- Writing to the file
- Deleting the file
Classes for file stream operation
#ofstream
Stream class to write on files.
Example of using ofstream in program code.
#include <iostream> #include <fstream> using namespace std; int main() { char data[100]; ifstream ifile; //create a text file before executing. ifile.open("text.txt"); while (!ifile.eof()) { ifile.getline(data, 100); cout << data << endl; } ifile.close(); return 0; }
See the following output.
#ifstream
Stream class to read from files.
Example of using ifstream in program code:-
#include <iostream> #include <fstream> using namespace std; int main() { char data[100]; ifstream ifile; //create a text file before executing. ifile.open("text.txt"); while (!ifile.eof()) { ifile.getline(data, 100); cout << data << endl; } ifile.close(); return 0; }
See the following output.
#fstream
It can both read and write from/to files.
#filebuf
Sets the file buffers to read and write. It contains close() and open() member function in it.
Opening and closing a file
Let’s Open the File by using the constructor.
#Open File by using constructor
See the following code example.
ifstream (constr char* filename, ios_base::openmode mode = ios_base::in); ifstream fin(filename, openmode) by default openmode = ios::in ifstream fin(“filename”);
#Open File by using the open() method
See the following code example.
Calling of default constructor ifstream filin; //create an input stream filin.open("filename", openmode); //associate filin stream with file . . filin.close(); //terminate association with file master
Different types of modes
in*
It stands for input and uses to open a file for reading. Support input operations.
out
It stands for output and uses to open a file from a writing point of view. Supports output operations*
binary
It stands for binary, and operations are performed in binary mode rather than in text format and anything else.
ate
It stands for at the end, and in this output, operations start at the end of the file.
app
It stands for append, and in this, all the output operations happen at the end of the file, appending to the existing files.
trunc
It stands for truncate, and with this, any content before the opening of the file is discarded.
All these flags can be combined with the help of the bitwise operator OR (|).
For instance, if we want to open a file example.scs in the binary mode to add data, we could do it by the following call to the member function open():
fstream file; file.open ("example.scs", ios::out | ios::app | ios::binary);
Default opening modes
#ifstream
ios::in
#ofstream
ios::out
#fstream
ios::in | ios::out
Closing File
See the following code.
stream_object.close(); fout.close(); fin.close();
INPUT AND OUTPUT OPERATION
#put() and get() function
The function put() is to write a single character to the associated stream.
The function get() reads a single character from the associated stream.
Example:
file.get(s); file.put(s);
#write() and read() function
The write() and read() functions write and read blocks of binary data(file).
See the following code.
file.read((char *)&a, sizeof(a)); file.write((char *)&a, sizeof(a));
STEPS TO PROCESS A FILE IN YOUR PROGRAM
- First, pin down the type of link required.
- Declare the stream for the desired type of link.
- Attach the desired file to the stream.
- Now process as required.
- Close the file link with the stream.
File Pointers
All the I/O streams objects have at least one internal stream pointer.
Ifstream is just like istream, which has a pointer called the get pointer to read in the next input by pointing to that element.
Ofstream is just like ostream, and it has a pointer called the put pointer, located at the points where the next element has to be written.
At last, stream, which inherits both the get and put pointers from iostream.
The other prototype for these functions is the following.
seekg(offset, refposition ); seekp(offset, refposition );
The refposition takes one of the following three constants defined in the ios class.
ios::beg
ios::cur ios::end |
Start of the file
The current position of the pointer End of the file |
Example:
file.seekg(-5, ios::beg)
Now, see the following table.
No. | Function | Description |
1 | fopen() | It opens a new or existing file |
2 | fprintf() | It helps to write data into the file |
3 | fscanf() | It reads data from the file |
4 | fputc() | It writes a character into the file |
5 | fgetc() | It reads a character from a file |
6 | fclose() | It closes the file |
7 | fseek() | It sets the file pointer to the given position |
8 | fputw() | It writes an integer to file |
9 | fgetw() | It reads an integer from a file |
10 | ftell() | It returns the current position |
11 | rewind() | It sets the file pointer to the beginning of the file. |
That’s it for file handling in C++.