Chapter Four: File I/O
Chapter Four: File I/O
File I/O
Introduction
• Data hierarchy
0 and 1 (bits) –> Charcaters --> field ->Record -> files
-> database
• Files:
– Is a collection of data that is stored together under a
common name usually on a secondary storage device
– E.g. the C++ programs that you store on a disk
• Why use files?
– Convenient way to deal with large quantities of data
– Store data permanently (until file is deleted)
– Avoid having to type data into program multiple times
– Share data between programs
Cont….
• Terminology
– Input. Get input from/read a file.
– Output. Put data into/write a file
• To store and retrieve data on a file in C++
three items are required:
– A file
– A file stream
– A mode
Cont….
• Files are physically stored on an external medium
using a unique name called external file name
• Streams is a one way transmission path that is
used to connect a file stored on a physical device
to a program.
• I/O in C++ uses streams… flow of data into and/or
out of a program
• A stream is a data type (like int, double), but with
special properties.
• Associated with every file stream is a mode, which
determines the direction of data on transmission
path
– To and from a file
Cont….
• Two file streams based on mode
– Input file stream – mode designated as input
– Output file stream – mode designated as
output
Disk
#include <fstream.h> input file stream
int main()
{
…. Output file stream
return 0;
}
Cont….
• there are two types of streams
– Text stream
• A text stream is a sequence of characters
• character translations may occur as required by the host
environment
– e.g.a new line may be converted to a carriage return/linefeed pair
• may not be a one-to-one relationship between the characters
that are written
– Binary stream
• A binary stream is a sequence of bytes
• a one-to-one correspondence to those in the external device
• To use files we need to know:
– how to "connect" file to program
– how to tell the program to read data
– how to tell the program to write data
– error checking and handling EOF
– How to “disconnect” file from program
Cont….
• You associate (connect) a stream
with a specific file by performing an
open operation
• Once a file is open, information can
be exchanged between it and a
program – read and write operation
Cont….
• You disassociate a file from a specific stream
with a close operation
– All files are closed automatically when the program
terminates normally
– Files are not closed when a program terminates
abnormally
• If you close a file opened for output, then
contents, if any, of its associated stream are
written to the external device
– this process is referred to as flushing the stream
• Each stream that is associated with a file has a
file control structure of type FILE
Cont….
• This structure FILE is defined in the header
stdio.h
• The File Pointer
– is a pointer to information that defines various
things about the file:
• name
• status
• the current position
• A file pointer is a pointer variable of type FILE.
– FILE * fp;
The standard streams
ios::ate Open a file for output and move to the end of the file
(normally used to append data to a file). Data can be
written anywhere in the file.
ios::binary Cause the file to be opened in binary mode.
ios::nocreate If the file does not exist, the open operation fails.
ofstream ios::out
ifstream ios::in
int main () {
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
• This code creates a file called example.txt and
inserts a sentence into it in the same way we are
used to do with cout, but using the file stream
myfile instead.
#include <iostream.h>
#include <fstream.h>
#include <string.h>
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{ while (! myfile.eof() )
{ getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
• reads a text file and prints out its content on the screen
Checking state flags
•There are functions that check the state of a stream with bool return
type
Function Description