Like Tree1Likes
  • 1 Post By Salem

Random access files

This is a discussion on Random access files within the C++ Programming forums, part of the General Programming Boards category; Hi..this is my coding. Its compiled successfully. But i could not get my result. I don't know whether the data ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53

    Random access files

    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.
    Code:
    #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


  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > if ( fPointer.write((char *) &acc, sizeof(struct status) ))
    Remove the &

    Ditto on reading as well.


    And get one of these for looking at your data files.
    http://en.wikipedia.org/wiki/Hex_editor
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by Salem View Post
    > if ( fPointer.write((char *) &acc, sizeof(struct status) ))
    Remove the &
    I removed. But still The result is not correct.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    Code:
    fPointer.open(fname.c_str(), ios::out | ios::binary);
    fPointer.read((char *) &acc, sizeof(struct status) )
    O_o

    Soma

  5. #5
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by phantomotap View Post
    Code:
    fPointer.open(fname.c_str(), ios::out | ios::binary);
    fPointer.read((char *) &acc, sizeof(struct status) )
    O_o

    Soma
    What you are telling??? sorry. i could not get u

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    It means do both of them!

    if ( fPointer.write((char *) &acc, sizeof(struct status) ))
    to
    if ( fPointer.write((char *) acc, sizeof(struct status) ))

    if ( fPointer.read((char *) &acc, sizeof(struct status) ))
    to
    if ( fPointer.read((char *) acc, sizeof(struct status) ))

    If that doesn't work, post your latest code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    Actually, I was referring to the fact that the file hasn't been opened for writing.

    Soma

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    Or reading
    phantomotap likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,260
    ^_^;

    Soma -> asleep at the wheel

  10. #10
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by Salem View Post
    It means do both of them!

    if ( fPointer.write((char *) &acc, sizeof(struct status) ))
    to
    if ( fPointer.write((char *) acc, sizeof(struct status) ))

    if ( fPointer.read((char *) &acc, sizeof(struct status) ))
    to
    if ( fPointer.read((char *) acc, sizeof(struct status) ))

    If that doesn't work, post your latest code.
    This is my new code :
    Code:
    #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 &)const;
        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::in | 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, "INFANT");
        acc.balance = 1000;
        acc.account_num = 3;
        return 0;
    }
    int FileHandler :: putRecords(struct status &acc)const
    {
        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);
        
        
    }

  11. #11
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    Well I see no file created when I run that code.

    I also see an error message when I add this after open
    Code:
        if ( !fPointer ) {
          cout << "Failed to open" << endl;
        }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by Salem View Post
    Well I see no file created when I run that code.

    I also see an error message when I add this after open
    Code:
        if ( !fPointer ) {
          cout << "Failed to open" << endl;
        }
    Then what is the problem here?? should i declare the fPointer as reference???

  13. #13
    Registered User
    Join Date
    Oct 2011
    Location
    India
    Posts
    53
    Quote Originally Posted by Salem View Post
    Well I see no file created when I run that code.

    I also see an error message when I add this after open
    Code:
        if ( !fPointer ) {
          cout << "Failed to open" << endl;
        }
    if i open like this, the file is created successfully.
    Code:
    fPointer.open(fname.c_str(), ios::out | ios::binary);
        if ( !fPointer ) 
        {
              cout << "Failed to open" << endl;
        }
    But i need to open as write and read mode.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-30-2008, 12:10 AM
  2. Random Access Files...ugh.
    By equss89 in forum C Programming
    Replies: 5
    Last Post: 07-21-2008, 02:25 PM
  3. i need help in random access files
    By backtolife in forum C++ Programming
    Replies: 3
    Last Post: 06-04-2006, 03:02 AM
  4. Random access files and structures
    By DLR in forum C Programming
    Replies: 8
    Last Post: 04-21-2006, 03:24 PM
  5. Random Access Files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 08:06 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21