Stream Error
Stream Error
This means that error messages written to cerr are usually displayed
immediately on the console
The primary advantage of using cerr for error messages is that it's
usually unbuffered, so it's more likely to display messages
immediately, even if the program crashes or encounters an unexpected
termination, helping users to quickly identify issues.
Example
9/3/20XX 5
Disk File I/O with stream
Working with disk files using streams in C++ involves the <fstream> header,
which provides classes for file input/output operations. There are mainly two
classes used for this purpose:
1]ifstream : This type of stream is used to read data from files.Provide input
operations. Contain open() with default input mode and inherits the function
get(),getline(),read(),etc.
2]ofstream :This type of stream is used to write data to files. Provide output
operations. Contain open() with default output mode and inherits the function
put(),write(),etc.
3]fstream :Provides support for simultaneous input and output
operations.Contains open with default input mode.Inherits all the functions from
istream and ostream classes through iostream.
Example of ifstream:
Example of ofstream:
File Pointers :
Every file maintains two pointers which tells the current position in
the file where reading or writing will takes place.
Syntax :
Seekp(offset, ios::seekdir dir);
Seekg(offset, ios::seekdir dir);
tellp and tellg return the current position of file pointer
seekp() and tellp() :
seekp() is a member function provided by the std::ostream class, and it is used to
set the position of the put pointer (output position indicator) within an output
stream. The put pointer indicates the position in the stream where the next
character will be written.
function is used with input streams, and returns the current “get” position of the
pointer in the stream.