Thread: File system errors

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    55

    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 ?

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    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.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    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 ...

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    In updateRecord() the client.accno is always 0. You are not updating the 'data' because of this. Which seems to be the problem.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    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.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  6. #6
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    how come in the updateRecord() .. the client.accno is always 0 ?

    how come destroying the file ? mind showing me the correct way ?

  7. #7
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    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.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM