File I/O Review:
- declare file stream object
- ifstream infile; // for file input - reading
- ofstream outfile; // for file output - writing
- fstream file; // this wll allow for reading or writing
to the attached file.
- open file
- infile.open("filename.ext");
- can delcare and open at same time
- ifstream infile("filename.ext");
- read from or write to file
- infile >> variablename;
- outfile << variablename;
- close file
Some Open mode Indicators
ios::app new data written to the file
is always appended to the end of the file.
EX: outfile.open("results.dat",ios::app)
ios::trunc when the file is
opened, if it exists all current data in it is deleted. (default if
ios::out is specified and ios::ate, ios::app, and
ios::in are not specified)
ios::ate when the
first byte of data is written to the file, it is appended to the end, but
subsequent bytes are written, to the current position
ios::nocreate if the file does not
already exist, the function fails (good to check for input file
existence)
EX: infile.open("data.txt",ios::nocreate)
//if you can't find it don't create one
outfile.open("results.txt",ios:app|ios::nocreate)
// open output file to append new output to bottom of file, don't
create one if can't find
ios::noreplace if the file already
exists, the function fails. (good if you want to make sure you
don't overwrite exisiting output file)
ios::binary opens the file
in binary mode (the default is text mode)
Checking for successful connection
to a file
infile.open("filename");
if (infile.fail())
// did the file fail to open?
{
cout<< "failed to open file"<<endl;
Note: the statement infile.open("filename")
on its own will create a file if one doesn't exist already
This is great for output files but we expect that our input files have
some data in the them for reading
To check that the input file exists prior to your trying to open it:
infile.open("filename", ios::nocreate); // try
to open the input file - don't create a new one if it's not found
if (infile.fail())
// was the input file found?
cout << "Input file not found"
<< endl;
Some "Methods" for stream
I/O
get(character-variable)
ex: infile.get(i)
will get the next character from the input stream and
place it in variable i
getline(string var, int n, '\n') extract characters
from the input stream until either n-1 characters
are read or a newline is encountered
ex: infile.getline(phrase,max,'\n')
peek(character-variable) place the next character
from the input stream in variable i put do not
extract that character
from the stream - leave it there
ex: infile.peek(x);
// will place the next character from the input stream
in variable x put also keep
it in the input
stream as well
put(character-variable) put
a single character on the output stream
ex: outfile.put(x)
// will place the character in variable x on the output
stream
eof()
returns true if a read is attempted past the EOF (end-of-file)
ex: infile.eof()
// if we are at EOF it will return
true otherwise false
ignore(int n)
Skip over the next n characters in
the stream . Good if you know you
exactly how much character data
you can ignore (jump over)
ex: infile.ignore(10)
// will skip the next 10 characters in the input file stream
ex: infile.ignore()
// will skip over the next 1 character in the input file
stream
NOTE: none of these functions skips white space
Remember:
manipulators - looked at earlier - setw, setprecision
etc hold for file i/o.
The file format of a file should never be a mystery
to you as a programmer. You must define or be given a file format
telling you how the
data will be presented in the file. Your job is to read
the data given a specific format. You cannot and do not have
to guess as to the format.
Text File --
all examples we are working with to date are text files.
Contains characters coded to the ASCII code (or another
if using another platform)
You can read the file - it is text
It has a line structure
consists of a number of lines
each line with an arbitrary number
of characters and is terminated with an end of line ('\n'}
Binary File
does not contain ASCII coded characters
An example would be an executable file
No line structure, only a sequence of bytes
No data conversion, read bytes, write bytes