0% found this document useful (0 votes)
14 views16 pages

Chapter 12 Data File Handling

Chapter 12 focuses on data file handling in C++, covering key concepts such as streams, file types, and member functions of file stream classes like ifstream and ofstream. It explains the differences between text and binary files, various file modes, and essential functions for reading and writing data. The chapter also highlights the advantages of using binary and text files for data storage.

Uploaded by

athmikarai09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views16 pages

Chapter 12 Data File Handling

Chapter 12 focuses on data file handling in C++, covering key concepts such as streams, file types, and member functions of file stream classes like ifstream and ofstream. It explains the differences between text and binary files, various file modes, and essential functions for reading and writing data. The chapter also highlights the advantages of using binary and text files for data storage.

Uploaded by

athmikarai09
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

CHAPTER 12 - DATA FILE HANDLING

CHAPTER 12 DATA FILE HANDLING

One marks questions:


1. What is a stream? (U)
ANS: A stream is sequence of bytes. In C++, a stream is a general name given to flow of
data.
2. Name the streams generally used for file I/O. (U)
ANS: Input Stream ,Output Stream, Error Stream
3. What are output streams? (U)
ANS: Output Stream: The stream that receives data from the program is known as output
stream.
4. What are input streams? (U)
ANS: Input Stream: The stream that supplies data to the program is known as input
stream.
5. What is a data file? (U)
ANS: A file is a collection of related data stored in a particular area on the disk.
6. Write the member functions belonging to fstream class. (U)
ANS: Open()
7. What is ifstream class? (U)
ANS: It supports input operations (read operations).
8. What is ofstream class? (U)
ANS: It supports output operations(write operations)..
9. Write any one member function belong to ofstream class. (U)
ANS: put( ), seekp( ), tellp( ) and write( )
10. Write any one member function belong to ifstream class. (U)
ANS: get( ), getline( ), read( ), seekg( ) and tellg( )
11. Name the stream class for file input in C++. (U)
ANS: ifstream
12. Name the stream class for file output in C++. (U)
ANS: ofstream
13. Mention the types of file. (U)
ANS: Text Files, Binary Files

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 1


CHAPTER 12 - DATA FILE HANDLING

14. What is text file? (U)


ANS: A text file is a file that stores the information in ASCII characters.

15. What is binary file? (U)


ANS: A binary file is a file that contains information in the same format as it is held in
memory.

16. What is the use of ios :: in? (U)


ANS: open file for reading
17. What is the use of ios::out? (U)
ANS: open file for writing
18. What is the use of ios::app? (U)
ANS: append to end of the file at opening time
19. What is the use of write( ) function? (U)
ANS: The write ( ) member function belongs to the class ofstream and which is used to
write binary data to a file.

20. What is the use of writeln( ) function? (U)


21. What is the use of get( ) function? (U)
ANS: function reads a single character from the associated stream.
22. What is the use of put( ) function? (U)
ANS: put( ) function writes single character to the associated stream.

23. What is the use of getline( ) function? (U)


ANS: getline( ) function is used to read a whole line of text.
24. What is the use of read( ) function? (U)
ANS: read( ) function is used to read binary data from a file.

25. What is the use of seekp( ) function? (U)


ANS: seekp( ) function move the put pointer to a specified location from the beginning of
a file.
26. What is the use of seekg( ) function? (U)
ANS: seekg( ) function move the get pointer to a specified location from the beginning of
a file.

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 2


CHAPTER 12 - DATA FILE HANDLING

27. What is the use of eof( ) function? (U)


ANS: eof( ) function returns a non-zero (true) value if the end of file condition is
encountered while reading; otherwise returns a zero (false).

Two marks questions:


1. Name the streams generally used for file I/O. (U)
ANS: Input Stream , Output Stream , Error Stream
2. What are input and output streams? (U)
ANS:
Input Stream: The stream that supplies data to the program is known as input stream.
Output Stream: The stream that receives data from the program is known as output
stream.
3. Mention the methods of opening file within C++. (U)
ANS: 1. Opening file using constructor 2. Opening file using open ( ) member function.

4. Write the member functions belong to fstream class. (U)


ANS: get( ), getline( ), read( ), seekg( ) , tellg( ), put( ), seekp( ), tellp( ) and write( )
5. Differentiate between ifstream and ofstream classes. (K)
ANS: Ifstream : It supports input operations. It contains open( ) with default input mode
and inherits get( ), getline( ), read( ), seekg( ) and tellg( ) functions from istream.
Ofstream : It supports output operations. It contains open( ) with default output mode
and inherits put( ), seekp( ), tellp( ) and write( ) functions from ostream
6. Differentiate between read( ) and write( ) functions. (K)
ANS: * The write ( ) member function belongs to the class ofstream and which is used to
write binary data to a file.

* The read ( ) member function belongs to the class ifstream and which is used to read
binary data from a file.

7. Differentiate between get( ) and getline( ) functions. (K)


ANS: The get( ) member function belong to the class ifstream and reads a single
character from the associated stream.

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 3


CHAPTER 12 - DATA FILE HANDLING

8. Write the member functions associated with ofstream class. (U)


ANS: put( ), seekp( ), tellp( ) and write( )
9. Write the member functions belong to ifstream class. (U)
ANS: get( ), getline( ), read( ), seekg( ) and tellg( )
10. Name the stream classes supported by C++ for file input and output. (A)
ANS: ifstream , ofstream, fstream
11. What are the advantages of saving data in binary file? (U)
ANS: Binary files are very much use when we have to deal with database consisting of
records.

exact internal representation.

There is no conversion while saving the data and hence it is faster.

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.

14. Why are get() and put()functions used? (U)


ANS: Refer above question.
15. What is the use of seekg( ) and seekp() functions? (U)
ANS: seekg( ) member function Move the get pointer to a specified location from the
beginning of a file.

seekp ( ) member function Move the put pointer to a specified location from the
beginning of a file.

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 4


CHAPTER 12 - DATA FILE HANDLING

16. Why are tellg( ) and tellp( ) member functions used? (A)
ANS: tellg( ) returns the current position of the get pointer.

tellp( ) returns the current position of the put pointer.

17. What is significance of fsream.h header file? (A)


ANS: The I / O system of C++ contains a set of classes which is designed to manage the
disk files.These include ifstream, ofstream and fstream.

stream.h and therefore we must include this file in any


program that uses files.
18. Differentiate between ifstream and ofstream classes. (K)
ANS: Refer below question
19. Explain any two file modes. (U)
ANS:
Mode method Meaning Stream Type
ios::app append to end of the file at opening ofstream
time
ios::in open file for reading ifstream
ios::out open file for writing ofstream

20. Differentiate between ios::in and ios::out. (U)


ANS:
ios::in open file for reading Its stream type is ifstream

Example : Example : fstream fin(“text”, ios::in); // open text in input mode


ios::out open file for writing Its stream type is ofstream

Example : fstreamfout (“text”, ios::out); // open text in output mode

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 5


CHAPTER 12 - DATA FILE HANDLING

Three marks questions:


1. What is a stream? Name the streams generally used for file I/O. (U)
ANS: Stream in C++:
 A stream is sequence of bytes. In C++, a stream is a general name given to flow of
data.
 Different streams are used to represent different kinds of data flow.
The three streams in C++ are as follows.
 Input Stream: The stream that supplies data to the program is known as input
stream.
 Output Stream: The stream that receives data from the program is known as output
stream.
 Error Stream: Error streams basically an output stream used by the programs to the
file or on the monitor to report error messages.

2. Write the member functions belong to fstream class. (U) .


ANS: get( ), getline( ), read( ), seekg( ) , tellg( ), put( ), seekp( ), tellp( ) and write( )
3. Explain: a. get( ) b. getline( ) c. read( ) (U)
ANS: get( ):

ifstream and reads a single character


from the associated stream.
Syntax: ifstream_object.get (ch); // where ch is the character variable.

char ch;
ifstream fin(“text.txt”);

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 6


CHAPTER 12 - DATA FILE HANDLING

fin.get (ch);

variable ch.

getline( ):

fin.getline(buffer, SIZE)

character is encountered, whichever comes first into the buffer.

Example:
char book[SIZE];
ifstream fin;
fin.getline (book, SIZE);

read ( ):

binary data from a file.

ifstream_object.read((char *) & variable, sizeof(variable));

ddress of the variable and second


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));

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 7


CHAPTER 12 - DATA FILE HANDLING

4. Mention the three modes of opening a file. (U)


ANS: There are three modes under which we can open a file:

 Read only mode


 Write only mode
 Append mode

5. Explain get( ) in data files. (U)


get( ):

ifstream and reads a single character


from the associated stream.

Syntax: ifstream_object.get (ch); // where ch is the character variable.

char ch;
ifstream fin(“text.txt”);
fin.get (ch);

variable ch.

6. Explain put( ) in data files. (U)


put ( ):

The put( ) member function belongs to the class ofstream and writes single character
to the associated stream.

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 8


CHAPTER 12 - DATA FILE HANDLING

ofstream_object.put(ch); // where ch is the character variable.

char ch=’A’;
ofstream fout(“text.txt”);
fout.put (ch);

7. Explain write( ) member function. (U)


ANS: write ( ):

binary data to a file.

ofstream_object.write((char *) & variable, sizeof(variable));

2 arguments. The first is the address of the variable and second


the size of the variable in bytes. The address of the variable must be type casted to pointer
to character.

student s;
ofstream fout(“std.dat”, ios::binary);
fout.write((char *) &s, sizeof(s));

8. Explain read( ) member function. (U)


read ( ):

binary data from a file.

ifstream_object.read((char *) & variable, sizeof(variable));

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 9


CHAPTER 12 - DATA FILE HANDLING

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( );

10. Write the syntax and example of close( ). (U)


ANS: stream_object.close( );
11. Explain different file modes. (K)
ANS: The lists of file modes are:
Mode method Meaning Stream Type
ios::app append to end of the file at opening time ofstream
ios::in open file for reading ifstream
ios::out open file for writing ofstream
ios::ate Open file for updating and move the file pointer to ofstream,
the end of file ifstream
ios::trunc On opening, delete the contents of file ofstream

ios::nocreate Turn down opening if the file does not exists ofstream

ios::noreplace Turn down opening if the file already exists ofstream

ios::binary Opening a binary file. ifstream ,

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 10


CHAPTER 12 - DATA FILE HANDLING

ofstream

Five marks questions:


1. What is a data file? Explain different types of data files. (U)
ANS: A file is a collection of related data stored in a particular area on the disk.
Types of data Files:

there are two types of files in C++:


Text Files:

o A text file is a file that stores the information in ASCII characters.

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.

2. Explain input and output operations on text files. (U)


ANS: Input and output operation in text file:

 put( ) function

 get( ) function
put ( ):

The put( ) member function belongs to the class ofstream and writes single character
to the associated stream.

ofstream_object.put(ch); // where ch is the character variable.

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 11


CHAPTER 12 - DATA FILE HANDLING

char ch=’A’;
ofstream fout(“text.txt”);
fout.put (ch);

get( ):

ifstream and reads a single character


from the associated stream.

Syntax: ifstream_object.get (ch); // where ch is the character variable.

char ch;
ifstream fin(“text.txt”);
fin.get (ch);

variable ch.

getline( ):

is used to read a whole line of text. It belongs to the class ifstream.

fin.getline(buffer, SIZE)

character is encountered, whichever comes first into the buffer.

Example:
char book[SIZE];
ifstream fin;
fin.getline (book, SIZE);

3. What are basic operations on binary files in C++? (U)

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 12


CHAPTER 12 - DATA FILE HANDLING

ANS: Input and output operation in binary files:


database consisting of
records.

exact internal representation.

in binary form are:


 write ( ) member function.
 read ( ) member function

write ( ):

binary data to a file.

ofstream_object.write((char *) & variable, sizeof(variable));

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 ( ):

binary data from a file.

ifstream_object.read((char *) & variable, sizeof(variable));

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 13


CHAPTER 12 - DATA FILE HANDLING

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:

 Input pointer (get pointer)


 Output pointer (put pointer)

place, appropriate pointer is automatically


advanced.

 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.

beginning so that we read the file from the beginning.

the existing contents are deleted and output


pointer is set at the beginning

This moves the file pointer to the end of the file.

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 14


CHAPTER 12 - DATA FILE HANDLING

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( );

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 15


CHAPTER 12 - DATA FILE HANDLING

int position
position= fin.tellg();

tellp( )

: position = ifstream_object.tellp( );

int position
position= fin.tellp();

Prepared by: Sanil Abraham , Deeksha Mahalakshmipuram Page 16

You might also like