0% found this document useful (0 votes)
27 views4 pages

Data File Handling - 093933

Computer science

Uploaded by

gogoiabinash300
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)
27 views4 pages

Data File Handling - 093933

Computer science

Uploaded by

gogoiabinash300
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/ 4

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

Lesson 07: DATA FILE HANDLING


Short Question/Answer
1. What is a File?
 A file is a bunch of bytes stored on some storage device like tape, or magnetic disk etc.
2. What is a stream?
 A stream is a sequence of bytes. The stream that supplies data to the program is known as input
stream and the stream that receives data from the program is known as output stream.
3. Name the classes associated with file stream?
 fstream, ifstream and ofstream
4. Name some of the member functions of the ifstream and ofstream class?
 ifstream class: get(), getline(),read(), seekg(), tellg() etc.
ofstream class: put(), write(), seekp(), tellp() etc.
5. Write one similarity & dissimilarity between ifstream and ofstream class?
 Similarity
Both the classes are derived from the header file iostream.h
Dissimilarity
The ifstream class provides input operations while, ofstream class provides output operations for file.
6. What do you mean by data files?
 The data files are the files that store data pertaining to a specific application, for later use.
7. How data files are stored in C++?
 i) Text files, that stores information in ASCII characters.
ii) Binary files, that stores information in the same format as it is held in memory.
8. Why binary files are faster and easier for a program to read and write?
 A binary file contains information in the same format in which the information is held in the memory.
There is no delimiter for a line and also no transaction occurs. Hence, binary files are faster and
easier for a program to read and write.
9. Write the two ways to open a file in C++?
 i) Using the constructor function of the ifstream class, e.g.,
ifstream file("filename");
ii) Using the open() function, e.g.
ifstream file;
file.open("filename");
10. Write the file modes for ifstream and ofstream class?
 ifstream class: ios::in, ios::ate, ios::binary
ofstream class: ios::out, ios::ate, ios::app, ios::trunc, ios::nocreate, ios::noreplace, ios::binary

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

11. Differentiate between -


a) ios::in and ios::out
 The ios::in mode opens a existing file for reading, while, ios::out creates a new file or opens an
existing file for writing by truncating the previous contents if any.
b) ios::ate and ios::app
 The ios::app mode lets to add the data at the end of the file, while, ios::ate mode allows to write
data anywhere in the file, even over the existing data.
c) ios::out and ios::app
 The ios::out creates a new file or opens an existing file for writing by truncating the previous
contents if any, while, ios::app creates a new file or opens an existing file for writing by
appending the data at the end of the file.
d) ios::nocreate and ios::noreplace
 The ios::nocreate mode causes the open() function to fail if the file does not pre-exist, while,
ios::noreplace mode causes the open() function fail if the file already exists.
12. Differentiate between -
a) seekg() and seekp()
 Both seekg(n) and seekp(n), places the cursor forwardly/backwardly to the specified n-bytes.
The only difference is that seekg() works with ifstream class, while, seekp(), works with ofstream
class.
b) tellg() and tellp()
 Both tellg() and tellp(), returns the position of file cursor in bytes, in the the file. The only
difference is that tellg() works with ifstream class, while, tellp(), works with ofstream class.
c) seekg() and tellg() or seekp() and tellp()
 seekg()/seekp() places the cursor forwardly/backwardly to the specified n-bytes, while,
tellg()/tellp() returns the position of file cursor in bytes, in the file.
Note: both seekg()/seekp() and tellg()/tellp() do the same work, only difference is seekg()/tellg() works with
ifstream and seekp()/tellp() works with ofstream.
13. Write the modes used with seekg() and seekp() to place the cursor.
 i) ios::beg i.e., from the begining of the file
ii) ios::cur i.e., from the current position (default) of the file
iii) ios::end i.e., from the end of the file
14. Differentiate between -
a) get() and put()
 Both functions get() and put() are byte-oriented. The only difference is that get() reads a byte
of data whereas put() write a byte of data.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

b) getc() and putc()


 The getc() function can read one character at a time from input stream, while, putc() function
write one character at a time to output stream.
c) get() and getline()
 The get() function does not extract the delimited newline character from the input stream i.e.
the delimiter character remains in the stream, while, getline() extracts the delimiter newline
character from the input stream so that the stream gets empty.
d) getc() and getline()
 The getc() function can read one character at a time, while, getline() can read a line of text of
specified size. The getc() function is defined in stdio.h, while, getline() is defined in iostream.h.
e) read() and write()
 The read() lets to read a unit of information from a file, while, write() lets to write a unit of
information to a file.
Conceptual Question/Answer
1. Explain briefly the streams used for file input-output operation?
 i) ifstream: It is derived from istream class and is used to associate a file with an input buffer so
that the file can be read from.
ii) ofstream: It is derived from ostream class and is used to associate a file with an output buffer
so that the file can be written onto.
iii) fstream: It is derived from iostream class and is used to associate a file with a buffer so that
the file can be read from as well as written onto.
2. Differentiate between a data file & a binary file.
 Text file Binary file
i) Stores information in ASCII characters. i) Stores information in raw format.
ii) Lines are text is delimited with EOL character. ii) No delimiters are used.
iii) Translations of EOL takes place. iii) No translation takes place.
iv) Slow processing than binary file. iv) Fast processing than text files.
3. Briefly explain the function of the following file mode constraints
 i) ios::in : Opens an existing file for reading.
ii) ios::out : Creates a new file or opens an existing file for writing, but discard the
previos contents it any.
iii) ios::ate : Seeks to the end-of-file upon opening of the file.
iv) ios::app : Causes all output appended to the end of the file.
v) ios::trunc : Destroys the contents of a pre-existing file by the same name and
truncates the file to zero length.
vi) ios::nocreate : Causes the open( ) function to fail if the file does not already exist.

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT
IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

vii) ios::noreplace : Causes the open( ) function to fail if the file already exist.
viii) ios::binary : Causes the file to be opened in binary mode.
4. Explain briefly the basic steps of file processing in C++.
 i) Determining the type of link required
ii) Declaration of a stream for the desired type of link
iii) Attach the desired file to the stream
iv) Process the file as required
v) Close the file-link with stream
***********************

IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUTORIAL POINT IP CS TUT

You might also like