Ses11 Fstream
Ses11 Fstream
3 Character I/O
4 Inheritance
Slide 6- 2
1
Streams and Basic File I/O
I/O Streams
n I/O refers to program input and output
n Input is delivered to your program via a stream object
n The keyboard
n A file
n The screen
n A file
Slide 6- 4
Objects
Slide 6- 5
Streams and Basic File I/O
n Files for I/O are the same type of files used to
store programs
n A stream is a flow of data.
n Input stream: Data flows into the program
n cout
n Output stream connected to the screen
Slide 6- 7
Why Use Files?
Slide 6- 8
File I/O
n Reading from a file
n Taking input from a file
n Writing to a file
n Sending output to a file
Slide 6- 10
Streams and Assignment
Slide 6- 11
Declaring An
Input-file Stream Variable
#include <fstream>
using namespace std;
#include <fstream>
using namespace std;
Slide 6- 13
Connecting To A File
n Once a stream variable is declared, connect it to
a file
n Connecting a stream to a file is opening the file
in_stream.open("infile.dat");
Double quotes
Period
Slide 6- 14
Using The Input Stream
Slide 6- 15
Using The Output Stream
n An output-stream works similarly to the
input-stream
n ofstream out_stream;
out_stream.open("outfile.dat");
Slide 6- 16
External File Names
your system
n Usually only used in the stream's open statement
Slide 6- 18
Objects
n An object is a variable that has functions and
data associated with it
n in_stream and out_stream each have a function named open
associated with them
n in_stream and out_stream use different
versions of a function named open
n One version of open is for input files
n A different version of open is for output files
Slide 6- 19
Member Functions
Slide 6- 20
Objects and
Member Function Names
name
Slide 6- 21
Classes
n ifstream is a class
member functions
n Example:
Slide 6- 23
Calling a Member Function
Calling_object .Member_Function_Name(Argument_list);
Slide 6- 25
Errors On Opening Files
Slide 6- 26
Catching Stream Errors
Slide 6- 27
Halting Execution
in_stream.open("stuff.dat");
if( in_stream.fail( ) )
{
cout << "Input file opening failed.\n";
exit(1) ;
}
Slide 6- 29
Techniques for File I/O
n When reading input from a file…
n Do not include prompts or echo the input
is lost
n To append new output to the end an existing file
n use the constant ios::app defined in the iostream
library:
outStream.open("important.txt", ios::app);
Slide 6- 33
Using A Character String
n char file_name[16];
cout << "Enter the file_name ";
cin >> file_name;
ifstream in_stream;
in_stream.open(file_name);
if (in_stream.fail( ) )
{
cout << "Input file opening failed.\n";
exit(1);
}
Slide 6- 34
Conclusion
n Can you
n Write a program that uses a stream called fin which
Slide 6- 37
Formatting Output to Files
n After out_stream.setf(ios::fixed);
All further output of floating point numbers…
n Will be written in fixed-point notation, the way we
normally expect to see numbers
n Calls to setf apply only to the stream named in
the call Slide 6- 40
setf(ios::showpoint);
n After out_stream.setf(ios::showpoint);
Slide 6- 41
Slide 6- 42
Creating Space in Output
7 7
Slide 6- 43
Not Enough Width?
cout.width(3);
when the value to print is 3456.45
n The entire item is always output
n If too few spaces are specified, as many more
Slide 6- 44
Unsetting Flags
cout.unsetf(ios::showpos);
Slide 6- 45
Manipulators
Slide 6- 46
The setw Manipulator
produces: Start 10 20 30
n Example: cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout << "$" << setprecision(2)
<< 10.3 << endl
<< "$" << 20.5 << endl;
produces: $10.30
$20.50
n setprecision setting stays in effect until changed Slide 6- 48
Manipulator Definitions
#include <iomanip>
using namespace std;
Slide 6- 49
Stream Names as Arguments
must be call-by-reference
Slide 6- 50
The End of The File
Slide 6- 51
End of File Example
n To calculate the average of the numbers in a file
n double next, sum = 0;
int count = 0;
while(in_stream >> next)
{
sum = sum + next;
count++;
}
Slide 6- 52
Stream Arguments
and Namespaces
n Using directives have been local to function
definitions in the examples so far
n When parameter type names are in a namespace
n A using directive must be outside the function so
cout << "*" << setw(3) << 12345 << "*" endl;
Slide 6- 58
3
Character I/O
Character I/O
usually automatic
Slide 6- 60
Low Level Character I/O
can devise
Slide 6- 61
Member Function get
n Function get
n Member function of every input stream
Slide 6- 62
Using get
char next_symbol;
cin.get(next_symbol);
Slide 6- 63
get Syntax
n input_stream.get(char_variable);
ifstream in_stream;
in_stream.open("infile.dat");
in_stream.get(next_symbol);
Slide 6- 64
More About get
n Given this code: char c1, c2, c3;
cin.get(c1);
cin.get(c2);
cin.get(c3);
and this input:
AB
CD
n c1 = 'A' c2 = 'B' c3 = '\n'
n cin >> c1 >> c2 >> c3; would place 'C' in c3
(the ">>" operator skips the newline character)
Slide 6- 65
The End of The Line
n To read and echo a line of input
n Look for '\n' at the end of the input line:
cout<<"Enter a line of input and I will "
<< "echo it.\n";
char symbol;
do
{
cin.get(symbol);
cout << symbol;
} while (symbol != '\n');
n All characters, including '\n' will be output
Slide 6- 66
'\n ' vs "\n "
n '\n'
n A value of type char
n "\n"
n A string containing only one character
Slide 6- 67
Member Function put
n Function put
n Member function of every output stream
n Does not do allow you to do more than previous output with the
Slide 6- 68
put Syntax
n Output_stream.put(Char_expression);
n Examples: cout.put(next_symbol);
cout.put('a');
ofstream out_stream;
out_stream.open("outfile.dat");
out_stream.put('Z');
Slide 6- 69
Member Function putback
Slide 6- 70
putback Example
n The following code reads up to the first blank in
the input stream fin, and writes the characters to
the file connected to the output stream fout
n fin.get(next);
while (next != ' ')
{
fout.put(next);
fin.get(next);
}
fin.putback(next);
n The blank space read to end the loop is put back into
the input stream Slide 6- 71
Program Example
Checking Input
n Incorrect input can produce worthless output
n If the input is not correct, allow the user to enter the data again
Slide 6- 72
Checking Input:
get_int
the input
n After displaying the input, get_int asks the user to
Slide 6- 74
'\n' Example
The Dialogue:
n The Code: Enter a number:
cout << "Enter a number:\n"; 21
int number; Now enter a letter:
cin >> number;
A
cout << "Now enter a letter:\n";
char symbol; The Result:
cin.get(symbol);
number = 21
symbol = '\n'
Slide 6- 75
A Fix To Remove '\n'
Slide 6- 76
Another '\n' Fix
Slide 6- 77
Detecting the End of a File
n Member function eof detects the end of a file
n Member function of every input-file stream
Slide 6- 78
Using eof
Slide 6- 80
How To Test End of File
n while ( ! in_stream.eof( ) )
numeric data
Slide 6- 81
Program Example:
Editing a Text File
Slide 6- 83
The toupper Function
Slide 6- 84
toupper Returns An int
Slide 6- 85
The isspace Function
n Example: if (isspace(next) )
cout << '-';
else
cout << next;
n Prints a '-' if next contains a space, tab, or
newline character
n See more character functions in
Slide 6- 86
Slide 6- 87
Slide 6- 88
Conclusion
n Can you
n Write code that will read a line of text and echo the line with all
n Describe whitespace?
Slide 6- 89
4
Inheritance
Inheritance
by adding features
n The class of input-file streams is derived from the
Slide 6- 91
Inheritance and Streams
Slide 6- 92
Stream Parameters Review
n Example:
void two_sum(ifstream& source_file)
{
int n1, n2;
source_file >> n1 >> n2;
cout << n1 " + " << n2
<< " = " << (n1 + n2) << endl;
}
n This code could be called using
ifstream fin;
fin.open("input.dat");
two_sum (fin);
Slide 6- 93
two_sum Is Not Versatile
n Suppose you wished to use function two_sum
with cin
n Since cin and input-file streams are both input
streams, this call to two_sum seems to make sense:
two_sum(cin);
Slide 6- 94
Fixing two_sum
Slide 6- 97
Inheritance Relationships
Slide 6- 98
Inheritance and Output
n ostream is the class of all output streams
n cout is of type ostream
Slide 6- 99
Program Example:
Another new_line Function
Slide 6- 103
Multiple Default Arguments
Slide 6- 105
Conclusion
n Can you
n Identify the types of cin and cout?
n Define object?
n Define class?
classes?
n List functions that format output?
Slide 6- 106
TUTORIAL 5
1. Write a program to print out the source code to the screen.
2. Still that program but to print the source code without comment to a file
no_comment.cpp.
3. Each character is one byte. Calculate the amount of memory in bytes only to
save the code of the program (of course without comment)
4. Print out the source code to the screen in the reverse order.
5. Write a program to read two large integer from file “input”. Calculate sum of the
two integers and print out the result to file “output”. The number of digits in one
integer varies from 1 to 1000000.
Two integers are separated by a space character.
Slide 6- 107