0% found this document useful (0 votes)
34 views

Stream Error

This document discusses stream errors in C++ and file input/output using streams. It covers: - The different stream objects like cin, cout, cerr, and clog and their uses - Using cerr for unbuffered error output - Reading and writing files using ifstream and ofstream classes - Maintaining file pointers with seekp(), tellp(), seekg(), and tellg() functions to set the position for reading/writing in a file.

Uploaded by

mayurb342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Stream Error

This document discusses stream errors in C++ and file input/output using streams. It covers: - The different stream objects like cin, cout, cerr, and clog and their uses - Using cerr for unbuffered error output - Reading and writing files using ifstream and ofstream classes - Maintaining file pointers with seekp(), tellp(), seekg(), and tellg() functions to set the position for reading/writing in a file.

Uploaded by

mayurb342
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Stream Error

Disk File I/O with stream


File Pointers
Stream Error :

Stream errors in C++ typically refer to issues or


problems that occur when performing input/output
(I/O) operations using streams.
cin ,cout ,cerr,clog
• cin : It is used to take input from standard input device.
• cout : It is used to show output on standard output device.
• cerr : It is standard error stream use to display errors . It is an
instance of ostream class . It is unbuffered standard
error
stream.
• clog : It is also an instance of ostream . It is also use to display
errors . It is buffered standard error stream.
cerr :
cerr refers to a standard error stream , which is an output stream used
for display error messages that should not be buffered .

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.

There are two types of functions :


1] seekp() ,tellp() : Used for output from program to files.
2] seekg() , tellg() : Used for input from files to program.

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.

seekp(offset, ios::seekdir dir);

There are 3 direction we use for offset value :


 ios::beg (offset from the beginning of the stream’s buffer).
 ios::cur (offset from the current position in the stream’s buffer).
 ios::end (offset from the end of the stream’s buffer).
tellp(): It function is used with output streams, and returns the current put
position of the pointer in the stream. It has no parameters and return a value of the
member type pos_type, which is an integer data type representing the current
position of the put stream pointer.
seekg() , tellg()
It is a function in the iostream library that allows you to seek to an arbitrary
position in a file. It is used in file handling to sets the position of the next character
to be extracted from the input stream from a given file .

function is used with input streams, and returns the current “get” position of the
pointer in the stream.

You might also like