File Managment
File Managment
File Managment
ofstream_ob.clear();
Once the reading of content of a file completes, the EOF(End of file) is reached
and the EOF flag is set on which does not allow us to access of file again for
reading it again.
Hence, a call to clear() function is very critically important because it sets the
EOF flag off, which allows us to re-read the content of file.
Prepared by Nega Teferra (M.Tech on CSE) 31
Binary File processing
For binary files, reading and writing data with the extraction and
insertion operators (<< and >>) and functions like getline is not
efficient, since we do not need to format any data and data is likely
not formatted in lines.
File streams include two member functions specifically designed to
read and write binary data sequentially: write and read.
The first one (write) is a member function of ostream (inherited by
ofstream).
And read is a member function of istream (inherited by ifstream).
Objects of class fstream have both. Their prototypes are:
write ( memory_block, size );
read ( memory_block, size );
Where memory_block is of type char* (pointer to char), and
represents the address of an array of bytes where the read data
elements are stored or from where the data elements to be written
are taken.
The size parameter is an integer value that specifies the number of
characters to be read or written from/to the memory block.
Prepared by Nega Teferra (M.Tech on CSE) 32
Example:- Read & Write Binary file
37
Prepared by Nega Teferra (M.Tech on CSE)