Streams and File IO Streams A stream is

  • Slides: 16
Download presentation
Streams and File I/O

Streams and File I/O

Streams • A stream is simply a sequence of bytes – The stream is

Streams • A stream is simply a sequence of bytes – The stream is an object with properties that are defined by a class • For input the bytes flow from a device into RAM • For output, the bytes flow from RAM to a device • It is up to the program to associate meaning to the bytes – They may represent ASCII, integers, graphics images, sound, etc.

Stream I/O • Many I/O features are object-oriented. Some OO features used include –

Stream I/O • Many I/O features are object-oriented. Some OO features used include – – references, function overloading operator overloading inheritance • Each I/O operation is type safe – It is automatically performed to handle a particular type of data – The I/O operation matches the type of the data – If there is no I/O operation for a type, the compiler signals an error

Library header files • The library contains many classes to handle I/O • All

Library header files • The library contains many classes to handle I/O • All of these are NOT put into just one header file, there are several header files for stream I/O • Each header file contains the class definitions for several classes

Common background of I/O Streams • The istream class defines input streams that can

Common background of I/O Streams • The istream class defines input streams that can be used to read data • The ostream class defines output streams that can be used to write data • Both of these are derived through single inheritance from the ios base class • The iostream class is ios derived through multiple inheritance from both the istream and the ostream istream class iostream

Global stream objects • The I/O library defines several global objects of type istream

Global stream objects • The I/O library defines several global objects of type istream and ostream. • These objects correspond to the standard I/O channels – cin (class istream) – cout (class ostream) – cerr (class ostream) – clog(class ostream)

istream class and objects • cin is an object of the istream class and

istream class and objects • cin is an object of the istream class and is “tied to” the standard input device • The stream-extraction operator (>>) is defined to put bytes into RAM from cin (the stream of bytes) • >> is overloaded to handle different data types • >> can also be overloaded to handle user-defined data types – i. e. to know how to input the classes you define

ostream class and objects • cout is one object of the ostream class and

ostream class and objects • cout is one object of the ostream class and is “tied to” the standard output device • The stream-insertion operator (<<) is defined to take bytes in RAM and put them in cout • << is overloaded to handle different data types • << can also be overloaded to handle user-defined data types • cerr is an object of the ostream class and is “tied to” the standard error device – This this stream is not buffered, so it appears immediately • clog is an object of class ostream – It is buffered

Input and output from files • Streams allow you to handle file I/O and

Input and output from files • Streams allow you to handle file I/O and standard I/O in a unified way • Just as cin is the stream coming from the keyboard, you can define a stream that comes from a file, or one that outputs to a file • The objects cin and cout are already declared for you. • The insertion and extraction operators (<<) and (>>) are binary operators • If you want a stream to connect to a file, you must declare it just as you would declare any other variable.

Declaring file stream objects • ifstream is a class derived from istream; input from

Declaring file stream objects • ifstream is a class derived from istream; input from a file is an object of this class – read( ) is a member function of ifstream expecting data of type char • ofstream is a class derived from ostream; output to a file is an object of this class – write( ) is a member function of ostream • If you want to input from a file you declare an object of type ifstream in_file; • You then use in_file in the same way you used cin – Example: in_file >> variable or –in_file. read(variable)

Connecting streams to file • The operating system must have a way to know

Connecting streams to file • The operating system must have a way to know which file connects to which stream object • This may be done in one of two ways – 1) When you declare the object, it is done by the constructor, using the file name as the parameter ifstream in_file(“myfile. txt”) – 2) Explicitly with the open member function after the object is declared in_file. open(“myfile. txt”)

Writing data to a file with the << operator • Constructors of the class

Writing data to a file with the << operator • Constructors of the class ofstream open the file when it is declared and initialized – ofstream out_file(“myfile. txt”) • If the file doesn’t exist, it is created. • If the file exists, the data in the file is overwritten by the new data • The data is always written sequentially, and it is not possible to back up • When the program terminates, its destructor closes the file

Classes and their functions • If you just want to do simple stream I/O

Classes and their functions • If you just want to do simple stream I/O (without formatting) either from the standard devices or from files, use the insertion (<<) and extraction(>>) operators • For greater control with I/O, use the member functions of the istream and ostream classes. • Remember that ifstream is derived from istream, so it has access to all the istream member functions • Same for ofstream and ostream. • The member functions are used with the object name, dot, member function name cin. get(ch) or in_file. get(ch)

The open( ) function • You can control whether a file will be opened

The open( ) function • You can control whether a file will be opened for reading, for writing, at the beginning or end of a file. • The mode bits control several aspects of file I/O – They are defined in ios, specifying various aspects of how a stream object will be opened – in – open the file for reading (default for ifstream) – out – open the file for writing (default for ofstream) – app – start writing at the end of file • It is used this way: fstream in. Out_file; in. Out_file. open(“my. File. txt”, ios: : app | ios: : in | ios: : out)

File Pointers • As a file is read or written to, the byte number

File Pointers • As a file is read or written to, the byte number in the file where the writing or reading will take place must be marked. • The file pointer marks this place • You can manipulate this file pointer around using member functions of istream – seekg( ) – set the distance from start of file – tellg( ) – return the position of the file pointer

Detecting end-of-file • Objects derived from ios contain error-status flags that return a zero

Detecting end-of-file • Objects derived from ios contain error-status flags that return a zero value if an error is detected – eof is one of these error conditions • So you can test for eof this way – while (in_file) do whatever – As long as the condition returns a nonzero value (which will be the address of in_file), an error condition like eof does not exist. • Or, test this way – while (!in_file. eof()) do whatever