.sigh.
The things I do....
This will show the file doesn't read trash and that the file is properly opened and that it truly reads from the file
(which it all did in the above version but wasn't evident enough to some)
...and there will be no fighting on the board
or I will call your mother :)
Code:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct applicant {
char name[61]; // applicant's name
char phone[21]; // phone number
long date; // date of application in yyyymmdd format
};
int main(void){
fstream fs("2111112.aaa", ios::in | ios::out | ios::binary);
applicant x;
x.date=9;
strcpy(x.name, "ginoitalo");
strcpy(x.phone, "13281");
cout<<endl<<"--------------"<<endl<<"Name="<<x.name<<endl;
cout<<"Date="<<x.date<<endl;
cout<<"Phone="<<x.phone<<endl<<"--------------"<<endl<<endl;
if(!fs.fail()){
fs.seekp(0, ios::beg);
fs.write( (char*)&x, sizeof(applicant) );
fs.close();
}
else{
exit(99);
}
// File is Totally Closed as of now.
// Re-open to prove it doen't read grabage.
fstream fs2("2111112.aaa", ios::in | ios::out | ios::binary);
if(!fs2.fail()){
fs2.seekg(0, ios::end);
cout<<"End of file = "<<fs2.tellg() <<endl;
cout<<"Size of applicant = "<<sizeof(applicant) <<endl;
int recs = ( 1*fs2.tellg() / sizeof(applicant) );
cout<<endl<<"Records in file="<<recs<<endl;
applicant in;
fs2.seekg(0, ios::beg);
fs2.read((char*)&in, sizeof(applicant) );
fs2.close();
cout<<endl<<"--------------"<<endl<<"Name="<<in.name<<endl;
cout<<"Date="<<in.date<<endl<<"Phone="<<in.phone<<endl;
cout<<"--------------"<<endl<<endl;
}
else{
exit(98);
}
return 0;
}