14.1-14.3 Introduction
Data Hierarchy: bits, bytes, fields, records,
files.
A bit is the smallest data item in a computer.
It can have a value of 0 or 1. (bit = binary digit)
A byte is eight bits. Characters are in bytes.
A field is a group of characters.
A records is composed of several fields.
A file is a group of related records.
Figure 14.1 on page 706.
14.3-14.4 File I/O
ofstream: output file stream, a class (a type).
ifstream: input file stream, a class (a type).
Simple Operations
// Create a sequential file
#include <fstream.h> //ofstream and ifstream are
defined in fstream.h
int main()
{
ofstream outFile( "mydata.txt", ios::out );
// mydata.txt resides on the harddisk.
// outFile is an object representing mydata.txt
in memory.
// outFile can be used in the same way as cout.
outFile << "Hi, this is a test.";
// The sentence is stored in mydata.txt for future
retrieval, not displayed.
outFile.close();
cout << "Enter a character to end: ";
char a;
cin >> a;
return 0;
}
// Read a sequential file
#include <iostream.h>
#include <fstream.h> //ofstream and ifstream are
defined in fstream.h
int main()
{
ifstream inFile( "mydata.txt", ios::in );
// mydata.txt resides on the harddisk.
// inFile is an object representing mydata.txt in memory.
// inFile can be used in the same way as cin.
char a[40];
inFile.getline(a,40); // A sentence
is retrieved from mydata.txt.
cout << "A line has been read from mydata.txt :" <<
endl;
cout << a <<endl;
inFile.close();
cout << "Enter a character to end: ";
cin >> a;
return 0;
}
Intermediate Operations
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
int main()
{
// ofstream constructor opens file
ofstream outClientFile( "clients.dat", ios::out );
if ( !outClientFile ) { // overloaded ! operator
cerr << "File could not be opened"
<< endl;
exit( 1 ); // prototype
in stdlib.h
}
cout << "Enter the account, name, and balance.\n"
<< "Enter end-of-file
to end input.\n? ";
int account;
char name[ 30 ];
float balance;
while ( cin >> account >> name >> balance ) {
outClientFile << account <<
' ' << name
<< ' ' << balance << '\n';
cout << "? ";
}
return 0; // ofstream destructor closes file
}
// Reading and printing a sequential file
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
void outputLine( int, const char *, double );
int main()
{
// ifstream constructor opens the file
ifstream inClientFile( "clients.dat", ios::in );
if ( !inClientFile ) {
cerr << "File could not be opened\n";
exit( 1 );
}
int account;
char name[ 30 ];
double balance;
cout << setiosflags( ios::left ) << setw( 10
) << "Account"
<< setw( 13 ) <<
"Name" << "Balance\n";
while ( inClientFile >> account >> name >> balance )
outputLine( account, name, balance );
return 0; // ifstream destructor closes the file
}
void outputLine( int acct, const char *name, double bal )
{
cout << setiosflags( ios::left ) << setw(
10 ) << acct
<< setw( 13 ) <<
name << setw( 7 ) << setprecision( 2 )
<< setiosflags( ios::fixed
| ios::showpoint )
<< bal << '\n';
}
Advanced File I/O Operations
File open modes:
ios::in Open
a file for input
ios::out Open a file for output
ios::app Write all output to the end of
the file
ios::ate Open a file for output and
move to the end of the file.
Data can be written anywhere in the file.
ios::trunc Discard the file's contents if it exists.
ios::nocreate If the file does not exist, the
open operation fails.
ios::noreplace If the file exists, the open operation
fails.
Repositioning the file position pointer :
file position pointer: the byte number of the next byte in the file
to be read or written.
istream: seekg
ostream: seekp
// Repositioning in a sequential file
#include <iostream.h>
#include <fstream.h> //ofstream and ifstream are
defined in fstream.h
int main()
{
char c;
char a[40];
ifstream data( "mydata.txt", ios::in );
data.getline(a,40); // A sentence is
retrieved from mydata.txt.
cout << "A line has been read from mydata.txt :" <<
endl;
cout << a << endl << endl;
data.close();
ifstream inFile( "mydata.txt", ios::in );
c = inFile.get();
cout << "The first byte in the file is: " << c <<
endl;
c = inFile.get();
cout << "The second byte in the file is: " << c
<< endl;
inFile.seekg(0); //
reposition the file pointer to the first byte (the begining of the file).
c = inFile.get();
cout << "The first byte in the file is: " << c <<
endl;
inFile.seekg(2); //
reposition the file pointer to the 3rd byte in the file.
c = inFile.get();
cout << "The third byte in the file is: " << c <<
endl;
cout << "After reading the third byte, the file pointer
is moved to the 4th byte (index 3). \n";
cout << "The file pointer is at index poitistion : ";
cout << inFile.tellg() << endl;
c = cin.peek();
cout << "The character at the position is : " <<
c << endl;
cout << "The 4th byte after " << c << " is
";
inFile.seekg(4,ios::cur);
// reposition the file pointer 4 bytes down.
c = inFile.get();
cout << c << endl;
inFile.seekg(-1,ios::end);
// reposition the file pointer to the last byte in the file.
c = inFile.get();
cout << "The last byte in the file is: " << c <<
endl;
inFile.seekg(2,ios::beg); // same as seekg(2); The default
is iso::beg.
c = inFile.get();
cout << "The third byte in the file is: " << c <<
endl;
cout << "Enter a character to end: ";
cin >> a;
return 0;
}
// Repositioning in a sequential file
#include <iostream.h>
#include <fstream.h> //ofstream and ifstream are
defined in fstream.h
int main()
{
char c;
char a[40];
ifstream data( "mydata.txt", ios::in );
data.getline(a,40); // A sentence is
retrieved from mydata.txt.
cout << "A line has been read from mydata.txt :" <<
endl;
cout << a << endl << endl;
data.close();
//Hi, this is a test.
//Ha, this is another test.
ofstream outFile( "mydata.txt", ios::ate );
outFile.seekp(1); // reposition the file pointer to the
first byte (the begining of the file).
outFile.put('a');
outFile.seekp(10,ios::cur); // reposition the file pointer
to the 10th byte from the current position.
outFile << "another test.";
outFile.close();
return 0;
}
EOF