100% found this document useful (1 vote)
22 views12 pages

Data File Handling Q&A

The document discusses file handling in C++. It provides answers to multiple choice and short answer questions related to file input/output streams, classes like ifstream, ofstream and fstream, member functions for reading and writing files, opening and closing files, and common file operations. Key topics covered include input/output streams, file classes and their functions, opening and reading/writing files, and handling file errors.

Uploaded by

riscomputersir
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
100% found this document useful (1 vote)
22 views12 pages

Data File Handling Q&A

The document discusses file handling in C++. It provides answers to multiple choice and short answer questions related to file input/output streams, classes like ifstream, ofstream and fstream, member functions for reading and writing files, opening and closing files, and common file operations. Key topics covered include input/output streams, file classes and their functions, opening and reading/writing files, and handling file errors.

Uploaded by

riscomputersir
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/ 12

Data File Handling

One Mark Questions

Question 1.
Which header file is required for file handling functions in C++?
Answer:
The header file required for file handling functions in C++ is <fstream.h>

Question 2.
What is stream?
Answer:
A stream is a sequence of characters that move from the source to the destination.

Question 3.
Name the streams generally used for file I/O.
Answer:
The streams generally used for file I/O is input stream, output stream, and error
stream.

Question 4.
What are output streams?
Answer:
A sequence of characters that move from computer to an output device like monitor.

Question 5.
What are input streams?
Answer:
A sequence of characters that move from input device like keyboard to the
computer.

Question 6.
Mention the methods of opening files within C++ program.
Answer:
The methods of opening file within C++ program

• Opening a file using constructor


• Opening a file using member function open()

Question 7.
Write the member functions belonging to fstream class.
Answer:
The member function belonging to fstream class is open() with default input mode.

Question 8.
What is ifstream class?
Answer:
This class supports input operations on files and is derived from istream class.

Question 9.
What is ofstream class?
Answer:
This class supports output operations on files and is derived from ostream class.

Question 10.
Write the member functions belonging to ofstream class.
Answer:
The member functions belonging to ofstream class are open () with default output
mode and inherits put(), write(), tellp() and seekp() from ostream.

Question 11.
Write the member functions belonging to ifstream class.
Answer:
The member functions belonging to ifstream class are open() with defauit input
mode and inherits get(), getiine(), read(), seekg() and tellg() from istream.

Question 12.
Name the stream classes supported by C++ for file input.
Answer:
The stream classes supported by C++ for file input are istream and ifstream.

Question 13.
Name the stream classes supported by C++ for output.
Answer:
The stream classes supported by C++ for output are ostream and ofstream.

Question 14.
Mention the file modes.
Answer:
The various file modes are ios::app, ios::ate, ios::binary, ios::in, ios:out etc.,

Question 15.
What is ios::in?
Answer:
The ios::in is a file mode that open the file in read-only mode.

Question 16.
What is ios::out?
Answer:
The ios::out is a file mode that open file for write-only mode.

Question 17.
Mention the types of files.
Answer:
The different types of files are binary files and text files.

Question 18.
What is text file?
Answer:
A text file that stores information in ASCII characters.

Question 19.
What is binary file?
Answer:
A binary file that contains information in the format as it is held in memory.

Question 20.
What is the use of write() function?
Answer:
The use of write() function is to write binary data to a file.

Question 21.
What is the use of writeln() function?
Answer:
Wrong question

Question 22.
What is the use of get() function?
Answer:
The get () function is used read a single character from the associated stream.

Question 23.
What is the use of put() function?
Answer:
The put() function is used to write a single character to the associated stream.
Question 24.
What is the use of getline() function?
Answer:
The use of getline() function is to read a whole line of text.

Question 25.
What is the use of read() function?
Answer:
The use of read() function is to read binary data from a file.

Question 26.
What is the use of seekp () function?
Answer:
The use of seekp() function to move the put pointer to a specified location from the
beginning of a file.

Question 27.
What is the use of seekg() function?
Answer:
The use of seekg() function to move the get pointer to a specified location from the
beginning of file.

Question 28.
What is the use of eof() function?
Answer:
The use of eof() function helps in detecting the end of file.

Question 29.
What is error handling function?
Answer:
Each bit such as eofbit, badbit, etc., are associated with the function that handles
appropriate errors. Such function are called error handling functions. For example,
eof() function.

Two Mark Questions

Question 1.
What is stream? Name the stream generally used for file I/O.
Answer:
A stream is a sequence of characters that move from the source to the destination.
The streams generally used for file I/O is input stream, output stream, and error
stream.
Question 2.
What are input and output streams?
Answer:
The input streams are sequence of characters that move from input devices like a
keyboard to the program/computer.
The output streams are sequence of characters that move from program/computer
to an output device like a monitor.

Question 3.
Mention the methods of opening file within C++. Discuss anyone.
Answer:
The methods of opening file within C++ program

• Opening a file using constructor


• Opening a file using member function open() of the class Opening a file using
constructor:
The syntax for opening file for output purpose only is ofstream obj(“filename”);
Example:
ofstream fout(“results.dat”);

Question 4.
Write the member functions belonging to fstream class.
Answer:
The member functions belonging to fstream class are constructor, open, is_open,
close, and inherits get(), getline(), read(), seekg() and tellg() from istream, inherits
put(), write() seekp() and tellp() from ostream.

Question 5.
Differentiate between ifstream class and ofstream class.
Answer:
The ifstream class supports input operations on file whereas ofstream class
supports output operations on files.

Question 6.
Differentiate between read() and write ().
Answer:
The read() member function belongs to the class ifstream and used to read binary
data from a file whereas write () member function belongs to the class ofstream and
is used to write binary data to a file.

Question 7.
Differentiate between get() and getline().
Answer:
The get() member function belongs to the class ifstream and is used to read a single
character from the associated stream whereas getline () function is a string I/O
function that is used to read a whole line of text on text files.

Question 8.
Write the member functions belonging to ofstream class.
Answer:
The member functions belonging to ofstream class are open() with default output
mode and inherits put(), write(), seekp() and tellp() from ostream.

Question 9.
Writ.e the member functions belonging to ifstream class.
Answer:
he member functions belonging to ifstream class are open() with default input mode
and inherits get(), getline(), read(), seekg() and tellg() from istream.

Question 10.
Name the stream classes supported by C++ for file input and output.
Answer:
The stream classes supported by C++ for file input and output are fstreambase,
ifstream, ofstream, fstream, and filebuf.

Question 11.
What are the advantages of saving data in binary form?
Answer:
The records in the database usually contain different data types. In such areas
saving data in binary form helps to optimize storage space and file I/O would be
faster when compared to text files.

Three Mark Questions

Question 1.
Mention the methods of opening file within C++ program. Discuss.
Answer:
The methods of opening file within C++ program

• Opening a file using constructor


• Opening a file using member function open() of the class
Opening a file using constructor:
The syntax for opening file for output purpose only is ofstream obj(“filename”);
where ofstream is a class and obj is a object of type ofstream and “filename” is any
valid name of a file to be opened for output purpose only.
Opening file using open():
The syntax for opening file for output purpose only ofstream
object.oper>(“filename”);
where ofstream is a class and object is of ofstream class type and filename is any.
valid file to be opened for output purpose only.

Question 2.
Differentiate between ifstream class and ofstream class.
Answer:

• The ifstream class supports input operations whereas ofstream class


supports output operations on file.
• The ifstream class inherits functions from istream whereas ofstream class
inherits functions from ostream.
• The inherited member functions of ifstream are get(), getline(), read(),
seekg() and tellg() whereas ofstream member function are put(), write(),
seekp() and tellp().
Question 3.
Differentiate between read() and write ().
Answer:

read() write()
belongs to the class ifstream belongs to the class ofstream
used to read binary data from a
used to write binary data to a file
file
Syntax: fin.read( (char *) Syntax: fout.write( (char *) &variable,
&variable, sizeof(variable)); sizeof(variable));

Question 4.
Differentiate between get() and getline().
Answer:

get() getline()
belongs to the class ifstream belongs to the class ifstream
a string I/O function that is
to read a single character from
used to read a whole line of
the associated stream
text to text files
Syntax: ifstream Syntax: fin.getline(buffer,
object.get(char); SIZE);

Question 5.
Name the stream classes supported by C++ for file input and output.
Answer:
The stream classes supported by C++ for file input and output

• fstreambase class provides facilities for file operations.


• Ifstream class supports read operations and consists of open () with default
input mode.
• ofstream class supports output operations and consists of open() with default
output mode.
• fstream() class supports both input and output operations. It contains open()
with default input mode.
• filebuf class sets the file buffer to read and write.

Question 6.
Mention the types of file. Explain any one.
Answer:
The text file and binary file are the two types of files in C++.
Text file:
A text file also called as ASCII file is a stream of characters that can be sequentially
processed in a forward direction by the computer. In text files, each line of text end
with a EOL (End of line) character and each file ends with a special character called
the EOF (End of file). The file can be edited using text editors like C++ editor,
notepad, vi editor in UNIX.

Question 7.
What are the advantages of saving data in binary form and text form?
Answer:
The advantages of saving data in binary form (any three):

• Stores the data in binary form


• Files can be either processed sequentially or randomly
• No delimiters are used for a line
• No internal translation takes place
• It takes less space to store data. For example, the integer 4564 occupies 2
bytes in memory.

The advantages of saving data in text form (any three):

• Stores the data in ASCII characters


• Files processed sequentially in the forward direction
• Delimiter EOL is used for End of line and EOF is used to mark the end of file
• Internal translation takes place during EOL and EOF
• It takes more space to store data. For example, the integer 4564 occupies 4
bytes in memory.

Five Mark Questions

Question 1.
What are input and output streams?
Answer:
The input streams are sequence of characters that move from input device like
keyboard to the program/computer. The output streams are sequence of characters
that move from program/computer to an output device like monitor.

The stream classes supported by C++ for file input and output
1. fstreambase:
class provides facilities for file operations and consists of open() and close()
member functions. This is base class for fstream, ifstream and ofstream.

2. Ifstream:
class supports read operations and consists of open () with default input mode and
inherits function get(), getline, read() , seekg(), tellg() from istream class defined
inside iostream.h file.

3. ofstream:
class supports output operations and consists of open() with default output mode. It
inherits the function put(), write(), seekp() and tellp() from ostream.

4. fstream():
class supports both input and output operations. It contains open() with default
input mode and inherits all the functions from istream and ostream classes through
iostream. filebuf class sets the file buffer to read and write.

Question 2.
What is the significance of fstream.h header file?
Answer:
The fstream.h header predefines a set or operations for handling files related to
input and output. It defines certain classes that help to perform file input and
output. For example, ifstream class links a file to the program for input and ofstream
class links a file to the program for output and fstream classifies a file to the
program for both input and output.

The file operations make use of streams as an interface between the programs and
the files, A stream is a sequence of bytes and general name given to flow of data.
Input stream and output stream represent different kind of data flow into the
memory or out of the memory. The C++ provides ofstream class to write on files,
ifstream class to read from files and fstream class to both read and write from/to
file.

These classes are derived from fstreambase and from those declared in the header
file ” iostream.h”. The classes ofstream, ifstream, and fstream are designed
exclusively to manage the disk files and their declaration are present in the header
file “fstream.h”. To make use of these classes, fstream.h is included in all the
programs which handle disk files.

Question 3.
Mention the methods of opening file within C++. Discuss.
Answer:
The methods of opening file within C++ program

• Opening a file using constructor


• Opening a file using member function open() of the class.
Opening a file using constructor:
The syntax for opening file for output purpose only is

ofstream obj(“filename”);

where ofstream is a class and obj is a object of type ofstream and “filename” is any
valid name of a file to be opened for output purpose only.
Example:

ofstream fout(“results.dat”);

Opening file using open():


The syntax for opening file for output purpose only

ofstream object.open(“filename”);

where ofstream is a class and object is of ofstream class type and filename is any
valid file to be opened for output purpose only.
Example:

ofstream obj;
obj.open(“text.dat”);

Question 4.
Differential between ifstream class and ofstream class.
Answer:
• The ifstream class supports input operations whereas ofstream class
supports output operations on file.
• The ifstream class inherits functions from istream whereas ofstream class
inherits functions from ostream.
• The inherited member functions of ifstream are get(), getline(), read(),
seekg() and tellg() whereas ofstream member function are put(), write(),
seekp() and tellp().

Question 5.
Differentiate between read() and write() with example.
Answer:

read() write()
belongs to the class ifstream belongs to the class ofstream
used to read binary data from a file used to write binary data to a file
Syntax: fin.read( (char *) &variable, Syntax: fout.write( (char *)
sizeof(variable)); &variable, sizeof(variable));
Example: fin.read( (char *) &s, Example: fout.write ((char *) &s,
sizeof(s)); sizeof(s));

Question 6.
Differentiate between get() and getline() with exmaple.
Answer:

get() getline()
belongs to the class ifstream belongs to the class ifstream
a string I/O function that is
to read a single character from
used to read a whole line of
the associated stream
text to text files
Syntax: ifstream Syntax: fin.getline(buffer,
object.get(char); SIZE);
Example: fin.getline(book,
Example: fin.get(ch);
SIZE);

Question 7.
Explain any three file modes.
Answer:
The file can be opened both for input and output operations using fstream class.
The syntax for opening a file with constructor is fstream object(“filename”, mode);
The syntax for opening a file with open() is fstream object.open(“filename”, mode);
Therefore, file can be opened using different modes. They are
ios::in opens file for reading only. For example, fstream fin(“te’xt.dat”, ios::in);
ios::out opens file for writing only. For example, fstream fout(“text.dat”, ios::out);
ios::app opens a file to append to end of file. For example, fstream
file.open(“text.dat”,ios::app);

Question 8.
Differentiate between ios::in and ios::out.
Answer:

ios::in mode ios::out mode


Opens file for reading only Open file for writing only
Ifstream is stream type Ofstream is stream type
Syntax: fstream Syntax: fstream
object.open(“filename”, object.open(“filename”,
ios::in); ios::out);
For example, fstream For example, fstream
fin.open(“text.dat”, ios::in); file.open(“text.dat”,ios::app);

You might also like