EC-241 Object-Oriented Programming
EC-241 Object-Oriented Programming
EC-241 Object-Oriented Programming
Programming
LECTURE 12
C++ File I/O
• A stream is a general name given to a flow of
data.
istream
ios ostream
ios
ifstream
ios iostream
ios ofstream
ios
fstream
ios
Disk File I/O with Streams
• Three relevant classes:
– ifstream for input // ifstream in;
– ofstream for output // ofstream out;
– fstream for both input and output
// fstream io;
Opening and Closing a file
• void ifstream::open(const char *filename, ios::openmode mode= ios::in)
• ios::in
• ios::out
• ios::binary
• ios::trunc
• Ios::app
• io.close();
Reading and writing Text Files
• Use << and >> operators the same way you do
when performing console I/O, except that
instead of using cin, cout substitue a string
that is linked to a file
Writing and Reading
#include <iostream> #include <iostream>
# include <fstream>
# include <fstream> #include <string>
using namespace std; using namespace std;
void main()
{
void main()
string item;
{ float price;
ifstream in("test.txt");
ofstream out("test.txt");
in>>item>>price;
out<<"Computer "<<28.9;
cout<<item<<" "<<price<<endl;
out<<"Laptop "<<59.5;
in>>item>>price;
cout<<item<<" "<<price<<endl;
} }
Formatted File I/O
• Numbers are stored as a series of characters
• E.g. 6.02 is stored as character ‘6’, followed by
chars ‘.’, ‘0’, and ‘2’.
• Inefficient, but easy to implement
Writing (Formatted) Data
# include <fstream>
#include <string>
…
void main()
{
char ch=‘x’; int i=77; double d=6.02;
string str1=“Kafka”, str2=“Proust”; //strings without embedded spaces
ofstream outfile(“fdata.txt”);
outfile<<ch
<<i
<<‘ ‘ //needs space btw numbers
<<d
<<str1
<<‘ ‘ //needs space btw strings
<<str2;
}
Reading (Formatted) Data
void main()
{
char ch; int i; double d;
string str1, str2;
ifstream infile(“fdata.txt”);
infile>>ch>>i>>d>>str1>>str2;
cout<<ch<<endl
<<i<<endl
<<d<<endl
<<str1<<endl
<<str2<<endl;
}
Strings with Embedded Blanks
//file output
…
void main()
{
ofstream outfile(“test.txt”);
outfile<<“I fear thee, ancient mariner!\n”;
outfile<<“I fear thy skinny hand\n”;
outfile<<“And thou art long, and lank, and brown, \n”;
outfile<<“As is the ribbed sea sand, \n”;
}
Strings with Embedded Blanks
//file input
…
void main()
{
const int MAX=100;
char buffer[MAX];
ifstream infile(“test.txt”);
while (!infile.eof()) // until eof encountered
{
infile.getline(buffer, MAX);
cout<<buffer<<endl;
}
}
Unformatted and Binary I/O
• In binary I/O numbers are stored as they are in
the memory rather than as strings of
characters
• In binary I/O int is always stored in 4 bytes,
whereas its text version might be ”234567”
Unformatted and Binary I/O
• put() and get() can be used to output and input
singal characters
• istream &get(char &ch)
• ostream &put(char ch)
os.write(reinterpret_cast<char*> (buff),
MAX*sizeof(int));
os.close();
Binary I/O
for (int j=0; j<MAX; j++)
buff[j]=0;
ifstream is(“edata.dat”, ios::binary);
int n=person::diskCount();
cout<<“There are “<<n<<“persons in the file”;
for (int j=0; j<n; j++)
{
cout<<“\n Person “ <<j;
p.diskIn(j);
p.showData();
}
}