Chapter 12 Data File Handling
Chapter 12 Data File Handling
* The read ( ) member function belongs to the class ifstream and which is used to read
binary data from a file.
12. What are the advantages of saving data in Text file? (U)
ANS: Text files can easily read or opened by any program.
We can easily recover the data if any data corruption occurs.
The ASCII standard allows ASCII- only text files to be freely interchanged and readable
in Unix, Macintosh, Microsoft Windows etc
13. Which are the character I/O operations used in files? (U)
ANS: The get( ) member function belong to the class ifstream and reads a single
character from the associated stream.
put( ) member function belongs to the class ofstream and writes single character
to the associated stream.
seekp ( ) member function Move the put pointer to a specified location from the
beginning of a file.
16. Why are tellg( ) and tellp( ) member functions used? (A)
ANS: tellg( ) returns the current position of the get pointer.
char ch;
ifstream fin(“text.txt”);
fin.get (ch);
variable ch.
getline( ):
fin.getline(buffer, SIZE)
Example:
char book[SIZE];
ifstream fin;
fin.getline (book, SIZE);
read ( ):
student s;
ifstream fin(“std.dat”, ios::binary);
fin.write((char *) &s, sizeof(s));
char ch;
ifstream fin(“text.txt”);
fin.get (ch);
variable ch.
The put( ) member function belongs to the class ofstream and writes single character
to the associated stream.
char ch=’A’;
ofstream fout(“text.txt”);
fout.put (ch);
student s;
ofstream fout(“std.dat”, ios::binary);
fout.write((char *) &s, sizeof(s));
the size of the variable in bytes. The address of the variable must be type casted to pointer
to character.
student s;
ifstream fin(“std.dat”, ios::binary);
fin.write((char *) &s, sizeof(s));
9. Explain close( ) member function. (U)
ANS:
file and the stream object.
stream_object.close( );
ofstream.close( );
ifstream.close( );
ios::nocreate Turn down opening if the file does not exists ofstream
ofstream
o Each line of text is terminated by a special character, known as End of Line (EOL) or
delimiter.
Binary Files:
o A binary file is a file that contains information in the same format as it is held in memory.
o In binary files, no delimiters are used for a line and no translations occur here.
put( ) function
get( ) function
put ( ):
The put( ) member function belongs to the class ofstream and writes single character
to the associated stream.
char ch=’A’;
ofstream fout(“text.txt”);
fout.put (ch);
get( ):
char ch;
ifstream fin(“text.txt”);
fin.get (ch);
variable ch.
getline( ):
fin.getline(buffer, SIZE)
Example:
char book[SIZE];
ifstream fin;
fin.getline (book, SIZE);
write ( ):
the size of the variable in bytes. The address of the variable must be type casted to pointer
to character.
Example:
student s;
ofstream fout(“std.dat”, ios::binary);
fout.write((char *) &s, sizeof(s));
read ( ):
the size of the variable in bytes. The address of the variable must be type casted to pointer
to character.
student s;
ifstream fin(“std.dat”, ios::binary);
fin.write((char *) &s, sizeof(s));
4. What is a file pointer? Explain the different member functions to manipulate data files.
(A)
ANS: File pointers and their manipulation:
ifstream, like istream, has a pointer known as get pointer that points to the element
to be read in the next input operation.
ofstream, like ostream, has a pointer known as put pointer that points to the location
where the next element has to be written.
5. Define the following terms: a. get( ) b. getline( ) c. read( ) d. write( ) e. put( ) (U)
ANS: Refer previous question..
6. Define the following terms: a. eof( ) b. seekg( ) c. seekp( ),d. tellg( ) e. tellp( ) (U)
ANS: End of file:
file.
-zero (true) value if the end of file condition is encountered while reading;
otherwise returns a zero (false).
ifstream fin;
if(fin.eof( ))
{ statements; }
seekg( ):
seekg(long);
seekg(offset, seekdir);
seekp ( ):
seekp(long);
seekp(offset, seekdir);
tellg ( )
position = ifstream_object.tellg( );
int position
position= fin.tellg();
tellp( )
: position = ifstream_object.tellp( );
int position
position= fin.tellp();