Page 294 - DCAP404 _Object Oriented Programming
P. 294
Unit 13: Working with Files
data is actually saved in the designated file on the disk. Once the data has been written to the Notes
disk the buffer becomes empty again.
In case you want to force the data be saved even though the buffer is not full without closing the
file you can use the flush() function. A call to flush() function forces the data held in the buffer to
be saved in the file on the disk and get the buffer empty.
Self Assessment
Fill in the blanks:
10. …………………………. returns the ASCII code of the current character from an input file
stream.
11. ………………….. are created and maintained in the RAM.
12. ……………….. is used to read one line at a time from an input stream until some specified
criterion is met.
13.4 Appending
Inserting data somewhere in a sequential file would require that the entire file be rewritten. It
is possible, however, to add data to the end of a file without rewriting the file. Adding data to
the end of an existing file is called appending.
Did u know? What happens when you open a file?
Files are cleared by default when you open them.
fout.open(“filename.dat”, ios::app) //open file for appending
//add names and ages to an existing file
#include <iostream.h>
#include <fstream.h>
If the file you open for appending does not exist, the operating system creates one just as if you
had opened it using ios::out mode.
int main(void)
{
apstring name, dummy;
int number, i, age;
ofstream fout;
cout<<“How many names do you want to add?”;
cin>>number;
getline(cin,dummy);
LOVELY PROFESSIONAL UNIVERSITY 287