Hi..this is my coding. Its compiled successfully. But i could not get my result. I don't know whether the data stored in the file successfully or not. Please Help me.
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct status
{
char name[80];
double balance;
unsigned long account_num;
};
class FileHandler
{
public:
FileHandler(const string&);
~FileHandler();
int getRecords(struct status *);
int putRecords(struct status *);
int ReadRecord(int ,struct status *);
int WriteRecord(int ,struct status *);
private:
string fileName;
fstream fPointer; // ref
};
FileHandler :: FileHandler(const string& fname)
{
fPointer.open(fname.c_str(), ios::out | ios::binary);
}
int FileHandler :: WriteRecord(int RecNum,struct status *acc)
{
if( fPointer.seekp(RecNum*sizeof(struct status), ios::beg) == 0 )
if ( fPointer.write((char *) &acc, sizeof(struct status) ))
return 1;
return 0;
}
int FileHandler :: ReadRecord(int RecNum,struct status *acc)
{
if( fPointer.seekg(RecNum*sizeof(struct status), ios::beg) == 0 )
if ( fPointer.read((char *) &acc, sizeof(struct status) ))
return 1;
return 0;
}
FileHandler :: ~FileHandler()
{
fPointer.close();
}
int FileHandler :: getRecords(struct status *acc)
{
strcpy(acc->name, "Heartly");
acc->balance = 1000;
acc->account_num = 3;
return 0;
}
int FileHandler :: putRecords(struct status *acc)
{
cout << acc->name << endl;
cout << "Account # " << acc->account_num;
cout.precision(2);
cout.setf(ios::fixed);
cout << endl << "Balance: $" << acc->balance<<endl;
return 0;
}
int main()
{
FileHandler fp("abc.cli");
struct status acc;
fp.getRecords(&acc);
fp.WriteRecord(0,&acc);
strcpy(acc.name, "");
acc.balance = 0;
acc.account_num = 0;
fp.ReadRecord(0,&acc);
fp.putRecords(&acc);
}
RESULT IS
heartly@client6:~/cppprograms$ ./a.out
Account # 0
Balance: $0.00