2. What is a File?
A file is bunch of bytes stored on some storage device
like tape, magnetic disk etc.
3. C++ File Classes
In C++, file input/output facilities are implemented using three
classes-
1. fstream: Stream class to both read and write from/to files.
2. ifstream: Stream class to read from files
3. ofstream: Stream class to write on files
4. fstream header file
The C++ input/output operations are very much similar to the
console input and output operations.
The file operations also make use of streams as an interface
between the programs and the files.
A stream is a general name given to a flow of data at the
lowest level.
A stream can be just defined as a sequence of bytes.
5. File Input Output Streams
From the above figure we can see that the stream that supplies data
to the program is known as input stream. It reads the data from the
file and hands it over to the program. The stream that receives data
from the program is known as output stream. It writes the received
data to the files.
6. Storage of Data Files
Data files can be stored in two ways-
1. Text file – This file stores information in ASCII characters. In text
files, each line of text is terminated with a special character
known as EOL(End of line) character.
2. Binary file - It contains information in the same format in
which the information is held in memory . In binary files , there
is no delimiter for the line. Also no translation occurs in binary
files.
7. Opening a File
A file must be opened before you can read from it or write to it.
Either 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.
Syntax-
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.
8. S.No Mode Flag & Description
1 ios::app
Append mode. All output to that file to be appended to the end.
2 ios::ate
Open a file for output and move the read/write control to the end of the file.
3 ios::in
Open a file for reading.
4 ios::out
Open a file for writing.
5 ios::trunc
If the file already exists, its contents will be truncated before opening the file.
10. When a C++ program terminates it automatically 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.
Syntax-
void close();
Closing a File
11. 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
#include <iostream.h>
#include <fstream . h>
using namespace std;
void main() {
ofstream MyFile("file1.txt"); // Create and open a text file
MyFile << "Files can be tricky, but it is fun enough!"; // Write to the file
MyFile. Close(); // Close the file
}
Sample Code
12. Handling Data Files
Data files in C++ can be handles in two ways
1. Sequential file handling –A sequential access is that in which the
records are accessed in some sequence, i.e., the information in the
file is processed in order, one record after the other. This access
method is the most primitive one.
2. Random File Handling - Random-access file is a term used to
describe a file or set of files that are accessed directly instead of
requiring that other files be read first. A random-access data file
enables you to read or write information anywhere in the file.
13. Sequential I/O with files
• The file stream classes support a number of member functions for performing the input and
output operations on files.
• The functions get() and put() are capable of handling a single character at a time.
• The function getline() lets you handle multiple characters at a time.
•Another pair of functions read() and write() are capable of reading and writing blocks of binary
data.
The get(), getline() and put() Functions The functions get() and put() are byte-oriented. That is,
get() will read a byte of data and put() will write a byte of data. The get() has many forms, but
the most commonly used version is shown here, along with put() :
istream & get(char & ch) ; //prototype of put()
istream & getline(char * buf, int num, char delim = 'n') ; //prototype of getline()
14. Random I/O with Files
The seekg(), seekp(), tellg() and tellp() Functions
In C++, random access is achieved by manipulating seekg(), seekp(), tellg() and
tellp() functions. The seekg() and tellg() functions allow you to set and examine
the get_pointer, and the seekp() and tellp() functions perform these operations on
the put_pointer.
The seekg() and tellg() functions are for input streams (ifstream) and seekp() and
tellp() functions are for output streams (ofstream). However, if you use them with
an fstream object then tellg() and tellp() return the same value. Also seekg() and
seekp() work the same way in an fstream object.