Data_file_handling-1
Data_file_handling-1
INTRODUCTION-KEY TERMS
C++ provides a rich set of operations for both unformatted and formatted I/O operation. These I/O
operation are implemented through iostream library.
A “stream” is a sequence of bytes.
If bytes flow from a device like keyboard, disk drive etc to main memory, then it is call input
operator.
If bytes flow from main memory to a device like display screen, printer etc, then it is called output
operator.
A file is a bunch of bytes stored on some storage device like tape or magnetic disk etc.
In C++ a file at its lowest level is interpreted simply as stream of bytes. File I/O library manages
the transfer of these bytes.
At user level, it consists of a sequence of possibly intermixed data types-character, arithmetic
values and class objects.
Fstream is the header file used for file in C++.
Fstream consists of a set of operations for handling files related to input and output.
The stream that supplies data to the program is known as input stream.
The stream that receives data from the program is known as output stream.
Fstream base is derived from the basic class ios.
“A file is a basic unit of storage in the secondary memory.”
A file can have related data which are identified by a ‘file name’ which is processed by the system
using the location of the file.
The data and information generated by a program as a result of processing is stored as files in the
secondary memory.
Programs are written to manipulate these files using a set of ‘file manipulation/handling function
defined in -> fstream, ifstream, ofstream.
1
Text file
In text file, each line of text is terminated with a special character known as EOF (end of file) or
determiner. It stores information in ASCII character.
Binary file.
A binary file, is a file that contains information in the same format in which the information is held
in memory. There is no special character (determiner) for a line. Also no translation occurs in
binary file.
Opening a file
A file must be opened first before data can be read from it or written to it. There are 2 ways to open
a file with stream object.
1. Using the constructor.
2. Using open() member function.
2. Output: opens the file “abc.dat” for output purpose using the objects of ofstream.
Syntax: ofstream object(“filename”);
Ex: ofstream outfile(“abc.dat”);
3. Input/output: opens the file “abc.dat” for both input and output purpose using the object of
fstream.
Syntax: fstream object(“filename”);
2
2. Output: opens the file “abc.dat” for output purpose using the function open() and the object of
the class ofstream.
Syntax: ofstream_object.open(“filename”);
Ex: out_file.open(“abc.dat”);
3. Input/output: opens the file “abc.dat” for the input and output purpose using the open() and the
object of the class fstream.
Syntax: fstream_object.open(“filename”);
Ex: in_out_file.open(“abc.dat”);
Closing a file
close() is used to remove the linkage between the file and stream object.
Syntax: stream_object.close();
Ex: file_out.close();
Note: the file can be opened in multiple modes which can be given using the bitwise operator (::).
3
Input and output operation in binary files
write(): writes binary data to a file.
Syntax: ofstream_object.write((char*)&var, sizeof(var));
Eg: student s;
ofstream_fout(‘std.dat”,ios::binary);
Fout.write((char*)&s, sizeof(s));
The EOF() function detects end of file. The function is true(non-zero) if EOF is reached,
otherwise return false.
Ex: if(fin.eof())
{
Statements;
……
}
Ex: while(!fin.eof())
{
Statements;
...…
}
Examples
1. File opened in ‘read’ mode.
C + +
Fptr
Fptr
4
3. File opened in ‘append’ mode.
C + +
Fptr
FILE POINTERS
The file pointer can be moved to any desired location, using the following functions supported by
file system.
1. seekg(): moves get pointer to a specified location. It is given in 2 ways.
a. seekg(long);
Ex: seekg(50);
Moves get pointer to a specified location from the beginning of the file.
b. seekg(offset, seekdir)
Where offset is the number of bytes the get pointer is to be moved from seek dir position.
The reference position “seekdir” can take the following values.
3. tellp(): gives the current position of the put pointer. [gives byte number]
Example: int position= fin.tellp();
4. tellg(): gives the current position of the get pointer. [gives byte number]
Example: int position= fin.tellg();
5
Frequently asked questions
1. Which is the header file required for handling functions in c++.
2. What is stream?
3. Name the stream used for file input/output.
4. What is input stream and output stream?
5. mention the different types of files.
6. What is the use of eof() ?
7. Differentiate between ifstream and ofstream fucntions.
8. Differentiate between read() and write().
9. What is purpose of get() and getline().
10. Explain the member functions belonging to ifstream() and ofstream().
11. Explain the different file modes in c++.