You are on page 1of 35

Lecture 13:

IO stream

Dr. Fadi Alzhouri

Concordia University

Created by Rose Gomar, presented by Fadi Alzhouri


Textbook (Copyright)

“C++ How to Program”, 10th edition, by Harvey and


Paul Deitel Print ISBN: 0-13-444823-5, Chapter 14.

11/15/2022 2
What we learn in Lecture 13

• IO stream class
• Istream
• Ostream
• fstream
• Reading a file
• Writing into a file
• Copying a file

11/15/2022 3
I/O Streams
• C++ standard libraries provide an extensive set of I/O capabilities
• C++ I/O occurs in streams
• A stream represents a sequence of data (in bytes)
• I/O operations from devices to memory and vice versa are often
bottleneck
• Wait for disk drive/keyboard input
• Low-level I/O capability
• Unformatted (not convenient for programmers)
• Byte-by-byte transfer
• High-speed, high-volume transfers
• High-level I/O capability
• Formatted
• Bytes grouped (into integers, characters, strings, etc.)
• Suitable for most I/O needs other than high-volume transfers

4
I/O Streams (cont.)
• iostream library
• Has header files with hundreds of I/O capabilities
• Declares basic services
• Contains definition of two data types
• istream: input stream that enables char input
• Common input variable (cin)
• ostream: output stream that enables char output
• Common output variable (cout)
• Unbuffered/buffered common error streams (cerr/clog)

• <iomanip>
• Formatted I/O with parameterized stream manipulators
• Example: fixed, setprecision
• <fstream>
• File processing operations (a.k.a. disk file IO)

5
Stream Classes and Objects

• #<iostream> means:
• cin
• cout
• cerr: is the unbuffered standard error stream which is used to output
the errors
• Clog: is the buffered standard error stream that is used to output the
errors.

6
Stream Classes and Objects

• << and >> are


• Stream insertion and extraction operators

• cin is a global object of istream


• Connected to standard input (usually keyboard)
cin >> grade;
• Compiler determines data type of grade
• cout is a global object of ostream
• Standard output (usually display screen)
cout << grade;
• As with cin, no type information needed

8
Input Stream

• get() member function


• grade = cin.get(); cin >> grade;
• Returns one character from stream (even whitespace)
• Returns EOF if end-of-file encountered

• End-of-file
• Indicates end of input
• ctrl-z on IBM-PCs

• ctrl-d on UNIX and Macs

cin.eof()
• Returns 1 (true) if EOF has occurred

9
Input Stream (cont.)

10
Input Stream (cont.)

• get(charRef)

• With character reference argument


• Gets one character, stores in charRef
• Returns reference to istream
• If EOF, returns -1

11
Input Stream (cont.)

Reference Argument

The return value is an istream


object

12
Input Stream (cont.)

13
Input Stream (cont.)

• get(charArray, size, delimiter)

• Reads until size-1 characters read, or delimiter


encountered
• Default delimiter '\n'
• Delimiter remains in input stream (i.e., not placed
in the character array)
• Can remove with cin.ignore()
• Makes array null-terminated

14
Input Stream (cont.)

Reads 7 char

15
getline Member Function

• Two syntaxes:

getline(array, size, delimiter)


• Like last version of get
• Reads size-1 characters, or until delimiter found
• Default \n
• Removes delimiter from input stream
• Puts null character at end of the character array
getline(cin, s, delimiter)
• S : string

16
getline Member Function (cont.)

Enter a sentence:
Using the getline member function

The sentence entered is:


Using the getline member function

17
getline Member Function (another
format)

Enter a sentence:
Using the getline member function

The sentence entered is:


Using the getline member function

18
getline() Vs. cin>>

19
Output Stream

• cout is a global object of ostream


• Standard output (usually display screen)
cout << grade;
• As with cin, no type information needed
cout << grade << endl;
• Endl flushes the output buffer.

20
Character Output

• put member function


• Outputs characters one at a time
cout.put('A'); cout << “A”;

• May be cascaded
cout.put('A').put('\n'); cout << “A” << “\n”;

• Dot operator (.) evaluates from left to right

• Can use numerical (ASCII) value


cout.put(65);

• Prints ‘A’ on the standard output

21
Character Output

The same

22
Character Output

correct
wrong

23
Character Output

24
Character Output

25
get and put

26
Files in C++
• A file is a collection of data stored on a disk
• C++ views a file simply as a sequence of bytes
• Each file ends with a special end of file marker

• Main operations on files


• Open a file
• Close a file
• Read from a file
• Save to a file
• Copy the content of one file to another
• Etc.

27
Creating Files in C++

• To create files in C++, you need to include the library


<fstream>

• fstream contains two important classes:


• ifstream (open primarily for input)
• Allows you to create an object that will represent the file from which
you read data

• ofstream (open primarily for output)


• Allows you to create an object that will represent the file to which
you write data

28
Opening a File for I/O (cont.)

• Opening a file to which we want to write data


ofstream outs;// create a file object outs of type ofstream
outs.open(“outData.txt"); // open the file

• Opening a file from which we want to extract data


ifstream ins; // Create a file object ins of type ifstream
ins.open(“inData.txt"); // open the file

• fail() function
• Returns true, nonzero, if file fails to open

• Note you can also specify the whole path of the file
• E.g., C:\\MyDir\\myfile.txt

29
Closing a File

• You must always close the file after utilization


• Particularly important when you intend to open many files

• Syntax – file could be of type ifstream or ofstream


file.close();

30
Example 1: Saving student grades
into a file

31
Example 2 Reading student grades
from a file

32
Example 3 Making a copy of a file

33
Example 3 Making a copy of a file
(cont.)

34
Example: I/O stream

Before input, cin.eof() is 0


Enter a sentence followed by end-of-file:
Testing the get and put member functions
Testing the get and put member functions Get returns -1
^Z

EOF in this system is: -1


After input cin.eof() is 1 35
Example: I/O stream

Enter a sentence:
Contrasting string input with cin and cin.get

The string read with cin was:


Contrasting

The string read with cin.get was:


string input with cin and cin.get
36

You might also like