-
File system errors
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct clientData
{
int accno;
int contact;
int choice;
string name;
string address;
};
class passenger
{
public:
passenger();
void newRecord();
void updateRecord();
void deleteRecord();
void showRecord();
private:
clientData blankClient;
clientData client;
};
passenger::passenger()
{
ofstream inOut("passenger.dat", ios::out);
blankClient.accno=0;
blankClient.contact=0;
blankClient.choice=0;
blankClient.name="";
blankClient.address="";
for(int i=0; i<100; i++)
{
inOut.write(reinterpret_cast<const char *>(&blankClient), sizeof(clientData));
}
inOut.close();
inOut.clear();
}
void passenger::newRecord()
{
fstream inOut("passenger.dat", ios::in | ios::ate);
int account;
cout << "Enter new account (1-100): ";
cin >> account;
inOut.seekg((account-1) * sizeof(clientData));
inOut.read(reinterpret_cast<char *>(&client), sizeof(clientData));
if(client.accno==0)
{
cout << "Enter name: ";
cin.ignore(1);
getline(cin, client.name);
cout << "Enter address: ";
cin.ignore(1);
getline(cin, client.address);
cout << "Enter contact number: ";
cin >> client.contact;
cout << "Enter flight choice (1-5): ";
cin >> client.choice;
client.accno=account;
inOut.seekp((account-1) * sizeof(clientData));
inOut.write(reinterpret_cast<const char *>(&client), sizeof(clientData));
}
else
cerr << "Account #" << account << " already contains information. " << endl;
inOut.close();
inOut.clear();
}
void passenger::updateRecord()
{
fstream inOut("passenger.dat", ios::in | ios::ate);
int account;
cout << "Enter account (1-100): ";
cin >> account;
inOut.seekg((account-1) * sizeof(clientData));
inOut.read(reinterpret_cast<char *>(&client), sizeof(clientData));
if(client.accno!=0)
{
cout << "Enter name: ";
cin.ignore(1);
getline(cin, client.name);
cout << "Enter address: ";
cin.ignore(1);
getline(cin, client.address);
cout << "Enter contact number: ";
cin >> client.contact;
cout << "Enter flight choice (1-5): ";
cin >> client.choice;
client.accno=account;
inOut.seekp((account-1) * sizeof(clientData));
inOut.write(reinterpret_cast<const char *>(&client), sizeof(clientData));
}
inOut.close();
inOut.clear();
}
void passenger::deleteRecord()
{
fstream inOut("passenger.dat", ios::in | ios::ate);
int account;
cout << "Enter account (1-100): ";
cin >> account;
inOut.seekg((account-1) * sizeof(clientData));
inOut.read(reinterpret_cast<char *>(&client), sizeof(clientData));
if(client.accno!=0)
{
blankClient.accno=0;
blankClient.contact=0;
blankClient.choice=0;
blankClient.name="";
blankClient.address="";
inOut.seekp((account-1) * sizeof(clientData));
inOut.write(reinterpret_cast<char *>(&blankClient), sizeof(clientData));
cout << "Account #" << account << "deleted." << endl;
}
}
int main()
{
passenger test;
test.newRecord();
test.updateRecord();
test.deleteRecord();
system("pause");
return 0;
}
when i run the program .... the updateRecord and deleteRecord seems like not working ... how to fix it ?
-
In deleteRecord() you open the file and seek to the end without seeking to the beginning, which is the same for updateRecord(). I'm not entirely sure but it seems to be you need to seek to the beginning or call the other version of seekg() that uses a second paramater and use ios::beg to start the seek from the beginning.
-
i tried to add ios::beg and seekg(0) .... but it still the same ...
i even try to change ios::ate to ios:app .. still the same ...
-
In updateRecord() the client.accno is always 0. You are not updating the 'data' because of this. Which seems to be the problem.
-
I just noticed the real root seems to be in your constructor. You are writing to the file but the way you open it destroys the file first.
-
how come in the updateRecord() .. the client.accno is always 0 ?
how come destroying the file ? mind showing me the correct way ?
-
It is always 0 because in your constructor you construct a 'blankClient' and initialize it to 0 and use it to write to the file with. When you open a file in the way you are doing it will destroy the contents of the file unless you use ios::ate (at end). In your constructor you have this line.
Code:
ofstream inOut("passenger.dat", ios::out);
That will destroy the file's contents and trunc it to 0 length first.