Sie sind auf Seite 1von 13

Declaring fstream Objects

An istream object named cin connects


program and keyboard These streams
These streams
are
areconstructed
constructed
automatically.
automatically.

An ostream object named cout connects


the program and the screen
Declaring fstream Objects
For doing I/O from/to a file a program must
explicitly open a stream
Creates a connection between a program in
memory and a text file
Basic fstream Operations
Must use #include <fstream>
open() Establishes connection program
to file
is_open() Returns true/false
>> Operator, inputs value from file
getline() Reads line of text into string
object
<< Operator, outputs value to file
eof() Returns true/false, end of file
close() Terminates connection between
program, file
The open() Operation
Given:
ifstream inStream;
inStream.open( "pressure.dat");
Parameter can also be a variable
If it is a string variable ( string fileName ) must
use fileName.data() for correct parameter type
When input file is opened, read position pointer
set to beginning of sequence of characters in the
file
The open() Operation (for output)
When output file is opened, file is created
on the disk, with write-position pointer
pointing at the eof marker

Opening an ofstream to a file will create a new


file
If file existed before, it is now (by default) destroyed
Otherwise, new file is created
The open() Operation (omit)
Possible to open the file with a mode
argument as a second parameter
Mode Description
open for input, non destructive, read
ios::in pointer at beginning
ios::trunc Open file, delete contents it contains
ios::out Open for output, use ios::trunc
Open for output, nondestructive, write
ios::app position at end of file
Open existing file with read (or write)
ios::ate position at end of file
ios::binary Open file in binary mode
Initialization at Declaration
Possible to open at declaration of variable
ofstream outStream ("pressure.out");
ifstream inStream ("pressure.in");

Executing
Program
Programming Defensively
The success or failure of a call to open a
file should always be tested
Use inStream.open()
Us in an assert( ) mechanism
Call before proceeding with additional
operations on the file
The Input Operator
We have used cin >> x;
Value entered via the keyboard
C++ uses the same operator to bring
values into variables from a stream
inStream >> reading;
The reading pointer keeps track of where
in the stream the program is currently
reading
The getline() Function
Requires an istream object, a string object
getline (nameStream, name);

Note: the>>
Note: the >>operator
operatordoes
doesnot not
Reads entire name into variableread
readthe
thenewline.
newline. The next>>
Thenext >>
skips
skipsititas
aswhite
whitespace.
space. But
Butififaa
Reads until it hits a newlinegetline
character usednext,
getlineisisused next,ititsees
sees
Newline character read, not the thenewline
added to and
newline andterminates.
variable
terminates.
Think
Thinkabout
aboutwhat
whathappens
happensififyou
you
mix>>
mix andgetline
>>and getlinecalls.
calls.
The eof() Message
Can be used as a sentinel value to control an input
loop
for ( ; ; ) {
inStream >> reading;
if (inStream.eof() ) break;
// . . . process the input }

inStream.eof() returns true following execution of


the input statement at this point
The Output Operator <<
Overloaded to perform with ostream,
ofstream objects
outStream <<"\n--> There were a total of"
<< count << "values.";

Note that the write pointer is pushed


forward, keeps pointing at the eof marker.
The close() Message
The file stream is disconnected when
The program leaves the scope of the
fstream object (implicitly)
The close() message is executed (explicitly)
It is good practice to explicitly close a file
when the program is done using it
If many files are accessed, the operating
system may place a limit on how many files
are open simultaneously

Das könnte Ihnen auch gefallen