File Input-Output in CPP
File Input-Output in CPP
Stream
A transfer of information in the form of a sequence of bytes
I/O Operations
Input stream: A stream that flows from an input device to the
main memory ( from: keyboard, disk drive, scanner or network
connection)
Output stream: A stream that flows from the main memory to an
output device ( to: screen, printer, disk drive or network
connection)
CHAPTER 4- FILE I/O OPERATIONS IN C++
The Interactive I/O Streams and your C++ Program
Program
Output Stream
Input Stream
(istream) (ostream)
I/O Devices
Program
Output Stream
Input Stream
(ifstream) (ofstream)
File on Disk
ios
istream ostream
fstream
Chapter 4- File I/O Operations in C++
Headers required for stream processing
o <fstream.h>
out_stream.open(“file.txt"
);
oit also deletes the previous contents of
the file
Chapter 4- File I/O Operations in C++
Creating and connecting streams to files in one statement
ifstream in_stream(“file.txt”);
ofstream out_stream(“file.txt”);
out_stream.close();
o Adds an "end-of-file" marker at the end of
the file, so if no data has been output to
“file.txt” since "out_stream" was connected
to it, we have this situation
Chapter 4- File I/O Operations in C++
Checking for Failure with File Commands
void main()
{
ifstream in_stream;
in_stream.open("Lecture.txt");
if (in_stream.fail()) // or if(!in_stream)
{
cout << "Sorry, the file couldn't be opened!\n";
exit(1);
}
//....
}
Chapter 4- File I/O Operations in C++
Character Input
Input using get()
we can extract or read single characters from the file using the member function "get(...)“
in_stream.get(ch); has two effects:
o the variable "ch" is assigned the value "'4'", and
o the ifstream "in_stream" is re- positioned so as to be ready to input the next
character in the file.
o Diagrammatically, the new situation is:
void main()
{
ofstream outClientFile( "clients.dat", ios::out );
if ( !outClientFile ) {
cerr << "File could not be opened”;
exit(1);
}
cout << "Enter the account, name, and balance.\n” << "Enter
end-of-file to end input.\n? ";
//contd on next slide…
fstream outClientFile;
int account; char name[30]; float balance;
outClientFile.open( "clients.dat",ios::out | ios::in );
//specifying multiple open modes
outClientFile>>account>>name>>balance;
cout<<account<<" "<<name<<" "<<balance;
outClientFile<<“writing something new!";
if ( !inClientFile ) {
cerr << "File could not be opened\n";
exit( 1 );
}
Chapter 4- File I/O Operations in C++
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 ) <<
resetiosflags( ios::left )<< setiosflags( ios::fixed |
CHAPTER 4- FILE I/O OPERATIONS IN C++
ios::showpoint )<< bal << '\n‘;}
Exercise
Design and implement a program that computes the average of a
sequence of numbers stored in a file
if (count > 0)
cout << “\nThe average of the values in “
<< inFileName << “ is “ << sum/count << endl;
else
cout << “\n*** No values found in file “
CHAPTER 4- FILE I/O OPERATIONS IN C++ << inFileName << endl;
}
Four types functions to manipulate File Pointers
seekg()
seekp()
tellg()
tellp()
//file: clientData.h
#include <string>
using namespace std;
struct clientData {
int accountNumber;
string lastName;
string firstName;
float balance;
};
Chapter 4- File I/O Operations in C++
// Creating a random access file
#include <iostream>
#include <fstream>
#include <stdlib>
#include "clientData.h"
void main()
{
ofstream outCredit ("credit.dat” , ios::binary);
if ( !outCredit ) {
cerr << "File could not be opened."
exit( 1 );
}
clientData blankClient = { 0, "", "", 0.0 };
if ( !outCredit ) {
cerr << "File could not be opened." << endl;
exit( 1 );
}
Chapter 4- File I/O Operations in C++
cout << "Enter account number”(1 to 100, 0 to end
input)\n? ";
clientData client;
cin >> client.accountNumber;
while ( client.accountNumber > 0 &&
client.accountNumber <= 100 ) {
cout << "Enter lastname, firstname, balance\n? ";
cin >> client.lastName >> client.firstName
>> client.balance;
outCredit.seekp( ( client.accountNumber - 1 )
*sizeof( clientData ) );
outCredit.write((const char*) &client ,
sizeof( clientData ) );
cout << "Enter account number\n? ";
cin >> client.accountNumber;
CHAPTER 4- FILE I/O OPERATIONS IN C++ }
Reading data from a random file sequentially
#include <iostream>
#include <iomanip>
#include <fstream>
#include <stdlib.h>
#include “clientData.h"
void outputLine( ostream&, const clientData & );
void main()
{
ifstream inCredit( "credit.dat", ios::in );
if ( !inCredit ) {
cerr << "File could not be opened." << endl;
exit( 1 );
}
clientData client;