11.1

What are input and output streams?

Answer

The stream that supplies data to the program is known as input stream and one that receives data from the program is known as output stream.

11.2

What are the steps involved in using a file in a C++ program?

Answer

The I/O system of C++ handles file operations which are very much similar to the console input and output operations. The input stream extracts data from the file and output stream inserts data to the file.

Review-question

11.3

Describe the various classes available for file operations.

Answer

See fig 11.3 of Balagurusamy, Page-325

11.4

What is the difference between opening a file with a constructor function and opening a file with open() function? When is one method preferred over the other?

Answer

When we open a file with a constructor a file name must be supplied during creation of object.

Example : ofstream outfile (“result”); //output only.

But when we use open ( ) function we can provide file name later.

Example :
ofstream outfile;
outfile.open (“Result”);

Another difference is we can provide file modes using open ( ) function.
stream-object open (“file name”, mode);
constructor method is proffered for only one file in the stream.
member function open ( ) of the class is preffered when we want to manage multiple files using one stream.

11.5

Explain how while(fin) statement detects the end of a file that is connected to fin stream

Answer

The given statement is
while (fin)
An ifstream object, such as fin, returns a value of o if any error occurs in the file operation including the end-of-file condition. We know while loop will terminate when fin returns zero. Thus end of file is detected.

11.6

What is a file mode? Describe the various file mode options available.

Answer

File-mode specifies the purpose for which the file is opened.

Various file mode options

Parameter                                                Meaning

ios::app                                                    Append to end of file

ios::ate                                                     Go to end of file on opening

ios::binary                                               Binary file

ios::in                                                       open file for reading only

ios::nocreate                                           open fails if the file does not exist

ios::noreplace                                        open fails if file already exist

ios::out                                                    open file for writing only.

ios::trunc                                                Delete the contents of the file if it exist.

 

11.7

Write a statement that will create an object called fob for writing, and associate it with a file name DATA\

Answer

ofsteam fob;
fob.open (“DATA”);

11.8

How many file objects would you need to create to manage the following situations?

  • (a) To process four files sequentially.
  • (b) To merge two sorted files into a third file. Explain.
Answer
  • (a) only a single stream object.
  • (b) In such case we need to create two separate input stream for handling the two input files and one output stream for handling the output file. That means require three stream object.
11.9

Both ios::ate and ios::app place the file pointer at the end of the file (when it is opened). What then, is the difference between them?

Answer

Both ios::ate and ios::app take us to the end of the file when it is opened. The difference between the two parameters is that the ios::app allows us to add data to the end of the file only. While ios::ate mode permits us to add data or to modify the existing data anywhere in the file.

11.10

What does the “current position” mean when applied to files?

Answer

The ‘curren position’ applied to file means the byte location at which the next read or write operation will take place.

11.11

Write statements using seekg() to achieve the following:

  • (a) To move the pointer by 15 positions backward from current position.
  • (b) To go to the beginning after an operation is over.
  • (c) To go backward by 20 bytes from the end.
  • (d) To go to byte number 50 in the file.
Answer

Let, fout be an ofstream object.

  • (a) fout.seekg (-15,ios::cur);
  • (b) fout.seekg(0,ios::beg);
  • (c) fout.seekg (-20,ios::end);
  • (d) fout.seekg (49,ios::beg);
11.12

What are the advantages of saving data in binary form?

Answer

The binary format is more accurate for storing the numbers as they are stored in the exact internal representation. There are no comuensions while saving the data and therefore saving is much faster read ( ) and write ( ) function do this job.

11.13

Describe how would you determine number of objects in a file. When do you need such information?

Answer

We can find out the total number of objects in a file using object_length as follows:
in n = file_size/object_length;
This information is required to

  • 1. Display the contents of a file.
  • 2. Modify an existing item
  • 3. Adding new item
  • 4. Deleting an existing item
11.14

Describe the various approaches by which we can detect the end•of-file condition successfully.

Answer

Let, fin be an object of if stream. If end of file occurs fin returns zero. So we can use while (fin) to detect end of file.

11.15

State whether the following statements are TRUE or FALSE.

  • (a) A stream may be connected to more than one file at a time.
  • (b) A file pointer always contains the address of the file.
  • (c) The statement
    outfile.wrttel(char *) & obj.sizeof(obj));
    writes only data in obi to outfit..
  • (d) The ios::ate mode allows us to write data anywhere in the file.
  • (e) We can add data to an existing file by opening in write mode.
  • (f) The parameter ios::app can be used only with the files capable of output.
  • (g) The data written to a file with write) function can be read with the get() function.
  • (h) We can use the functions tellp() and tellg() interchangeably for any file.
  • (i) Binary files store floating point values more accurately and compactly than the text files.
  • (j) The fin.fail()call returns non-zero when an operation on the file has failed.
Answer
  • (a) FALSE
  • (b) FALSE
    * file pointer can be a synonym for current position.
  • (c) TRUE
  • (d) TRUE
  • (e) TRUE
  • (f) TRUE
  • (g) FALSE
  • (h) FALSE
    * tell ( ) is for output file and tell g ( ) for input file
  • (i) TRUE
  • (j) FALSE
    * fin.fail ( ) cau returns non-zero when an input or output operation on the file has failed.

 


Next Previous