Professional Documents
Culture Documents
istream
istream ostream
ostream iostream
iostream
ifstream
ifstream ofstream
ofstream fstream
fstream
Opening and Closing a File
•Prototype:
void ifstream::open(const char *filename, ios::openmode mode = ios::in);
void ofstream::open(const char *filename, ios::openmode mode = ios::out | ios::trunc);
void fstream::open(const char *filename, ios::openmode mode = ios::in | ios::out);
ofstream out;
out.open("test", ios::out);
(Or)
out.open("test");
• Prototype:
– istream &get(char &ch);
– ostream &put(char ch);
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int i;
ofstream out("CHARS", ios::out | ios::binary);
if(!out)
{
cout << "Cannot open output file.\n";
return 1;
}
// write all characters to disk
for(i=0; i<256; i++) out.put((char) i);
out.close();
return 0;
}
read( ) and write( )
• to read and write blocks of binary data use read() and
write() functions.
• Prototype:
• Prototype:
– istream &seekg(off_type offset, seekdir origin);
– ostream &seekp(off_type offset, seekdir origin);
get pointer ->specifies where in the file the next i/p operation will occur.
put pointer ->specifies where in the file the next o/p operation will
occur.
1. iostate rdstate( );
2. Any one of the foll functions can be used,
• bool bad( );
• bool eof( );
• bool fail( );
• bool good( );
• Prototype:
– void clear(iostate flags=ios::goodbit);
#include <iostream> void checkstatus(ifstream &in)
#include <fstream> {
using namespace std; ios::iostate i;
void checkstatus(ifstream &in); i = in.rdstate();
int main(int argc, char *argv[]) if(i & ios::eofbit)
{ cout << "EOF encountered\n";
if(argc!=2) { else if(i & ios::failbit)
cout << "Usage: Display <filename>\n"; cout << "Non-Fatal I/O error\n";
return 1; else if(i & ios::badbit)
} cout << "Fatal I/O error\n";
ifstream in(argv[1]); }
if(!in) {
cout << "Cannot open input file.\n";
return 1;
}
char c;
while(in.get(c)) {
if(in) cout << c;
checkstatus(in);
}
checkstatus(in); // check final status
in.close();
return 0;
}
Customized I/O and Files
• Since the stream operations/functions are
similar, the same inserters & extractors
used for console I/O operations can be
used for files also.
#include <iostream> // Display name and phone number.
#include <fstream> ostream &operator<<(ostream &stream, phonebook o)
#include <cstring> {
using namespace std; stream << o.name << " ";
stream << "(" << o.areacode << ") ";
class phonebook stream << o.prefix << "-";
{ stream << o.num << "\n";
char name[80]; return stream; // must return stream
char areacode[4]; }
char prefix[4];
char num[5]; // Input name and telephone number.
istream &operator>>(istream &stream, phonebook &o)
public: {
phonebook() { }; cout << "Enter name: ";
phonebook(char *n, char *a, char *p, char *nm) stream >> o.name;
{ cout << "Enter area code: ";
strcpy(name, n); stream >> o.areacode;
strcpy(areacode, a); cout << "Enter prefix: ";
strcpy(prefix, p); stream >> o.prefix;
strcpy(num, nm); cout << "Enter number: ";
} stream >> o.num;
cout << "\n";
friend ostream &operator<<(ostream &stream, return stream;
phonebook o); }
friend istream &operator>>(istream &stream,
phonebook &o);
};
int main()
switch(c) {
{
case '1':
phonebook a;
cin >> a;
char c; cout << "Entry is: ";
fstream pb("phone", ios::in | ios::out | ios::app); cout << a; // show on screen
if(!pb) pb << a; // write to disk
{ break;
cout << "Cannot open phone book file.\n"; case '2':
return 1; char ch;
} pb.seekg(0, ios::beg);
for(;;) while(!pb.eof())
{ {
do pb.get(ch);
if(!pb.eof()) cout << ch;
{
}
cout << "1. Enter numbers\n";
pb.clear(); // reset eof
cout << "2. Display numbers\n";
cout << endl;
cout << "3. Quit\n";
break;
cout << "\nEnter a choice: ";
cin >> c;
case '3':
pb.close();
} while(c<'1' || c>'3');
return 0;
}
}
}
• The overloaded << operator can be used
to write to a disk file or to the screen
without any changes.