Beruflich Dokumente
Kultur Dokumente
Lecture # 19
Outline
Object I/O
I/O with Multiple Objects
The Mode Bits
File Pointers: get and put
Error Handling in File I/O
Object I/O
We can also write objects, of user defined
classes, to files.
Similarly, we can read objects from files.
When writing an object we generally want to
use binary mode.
This writes the same bit configuration to disk that
was stored in memory
and ensures that numerical data contained in
objects is handled properly.
3
cin>>name;
class Person
cin>>age;
private:
void showData()
char name[40];
int age;
cout<<name<<endl
public:
<<age<<endl;
void readData()
{
cout<<"Enter name: ";
}
};
os.write(reinterpret_cast<cha
r*>(&p), sizeof(Person));
#include<fstream>
os.close();
#include"person.h"
#include<string>
using namespace std;
Person p1;
int main()
ifstream is("person.dat",
ios::binary);
{
Person p;
p.readData();
ofstream os("person.dat",
ios::binary);
is.read(reinterpret_cast<char*
>(&p1), sizeof(Person));
p1.showData();
return 0;
}
int main() {
char ch;
Person p;
fstream file;
file.open("group.dat",ios::app | ios::out | ios::in |
ios::binary);
do{
p.readData();
file.write(reinterpret_cast<char*>(&p), sizeof(Person));
cout<<"Enter another person(y/n)? : "<<endl;
cin>>ch;
} while(ch=='y');
Continues
Continues
File Pointers
10
Example
12
seekg(-10, ios::end);
Begin
End
Begin
Begin
13
Current position
End
14
int main() {
Person p;
ifstream file;
file.open("group.dat", ios::binary);
file.seekg(0,ios::end);
int endposition=file.tellg();
int n=endposition/sizeof(Person); //no. of persons
cout<<"Number of persons in file: "<<n<<endl;
cout<<"Enter person no. ";
cin>>n;
int position=(n-1)*sizeof(Person);
file.seekg(position); //bytes from start
file.read(reinterpret_cast<char*>(&p), sizeof(Person));
p.showData();
return 0;
}
15
Reacting to Errors
16
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main() {
const int MAX=100;
int buff[MAX];
int j;
for(j=0; j<MAX; j++)
buff[j]=j;
ofstream os;
os.open("a:edata.dat", ios::trunc|ios::binary);
if(!os)
{cerr<<"could not open output file\n"; return;}
cout<<"writing..."<<endl;
os.write(reinterpret_cast<char*>(buff), MAX*sizeof(int));
Reacting to Errors
17
if(!os)
{cerr<<"could not write to file\n"; return;}
os.close();
for(j=0; j<MAX; j++)
buff[j]=0;
ifstream is("a:edata.dat", ios::binary);
if(!is)
{cerr<<"could not open input file\n"; return;}
cout<<"reading..."<<endl;
is.read(reinterpret_cast<char*>(buff), MAX*sizeof(int));
if(!is)
{cerr<<"could not read from file\n"; return;}
for(j=0; j<MAX; j++){
if(buff[j]!=j)
{cerr<<"data is incorrect"<<endl; return;}
}
cout<<"data is correct"<<endl;
}
Reacting to Errors
In this example we determined whether an error
occurred in an I/O operation by examining the return
value of the entire stream object.
Example
if(!is)
//error occurred
is returns a pointer value if everything went well, but 0
if it didnt.
No matter what the errors is, its detected in the same
way and the same action is taken.
18
Analyzing Errors
It is also possible, using the ios error-status
flags, to find out more specific information
about a file I/O error.
19
Analyzing Errors
void main() {
ifstream is;
is.open("data.dat", ios::binary);
if(!is)
{cerr<<"can't open data.dat file\n";}
else
cout<<"file opened successfully"<<endl;
cout<<"file = "<<is<<endl;
cout<<"error state = "<<is.rdstate()<<endl;
cout<<"good() = "<<is.good()<<endl;
cout<<"eof() = "<<is.eof()<<endl;
cout<<"fail() = "<<is.fail()<<endl;
cout<<"bad() = "<<is.bad()<<endl;
}
20