CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2009
    Posts
    326

    Random access file using fstream

    Hi,

    I wanted to do the following using fstream:
    1) Write 3 employee records into a new file - was able to do this
    2) Then the first character at the beginning of the 2nd record needs to be replaced with 'Z' - Wasn't able to this, I wasn't able to position seekp() correctly.

    Pls let me know where I went wrong and why.

    Given below is the program:

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    using std :: string;
    using std :: ostream;
    using std :: cout;
    using std :: endl;
    using std :: fstream;
    using std :: ios;
    
    class EmpRecord
    {
        public:
            EmpRecord(const int pEid, const string pName, const double pSalary);
            static int getSize();
       
            const int eid;
            const string name;
            const double salary;
    };
    
    ostream& operator << (ostream& pOutStream, const EmpRecord& pEmpRecord);
    
    int main()
    {
        EmpRecord emp1(1, "abcd", 100.20),
                  emp2(2, "efgh", 231.31),
                  emp3(3, "ijkl", 200.93);
    
        fstream destFile("outfile.txt", ios :: out);
        
        system("clear");
        
        cout << emp1 << emp2 << emp3;
        cout << "size of EmpRecord = " << EmpRecord :: getSize() << endl;
        
        destFile << emp1 << emp2 << emp3;
    
        destFile.seekp(EmpRecord :: getSize() - 1);
        destFile.put('Z');
        
        destFile.close();
        
        return(0);
    }
    
    EmpRecord :: EmpRecord(const int pEid, const string pName, const double pSalary)
        : eid(pEid), name(pName), salary(pSalary)
    {}
    
    int EmpRecord :: getSize()
    {
        int sizeInBytes = sizeof(int) + sizeof(string) + sizeof(double) + 2 * sizeof('\t') + sizeof('\n');
        return(sizeInBytes);
    
    }
    
    ostream& operator << (ostream& pOutStream, const EmpRecord& pEmpRecord)
    {
        pOutStream << pEmpRecord.eid << '\t'
                   << pEmpRecord.name << '\t'
                   << pEmpRecord.salary << '\n';
    
        return(pOutStream);
    }
    Thanks,
    Muthu

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Random access file using fstream

    1) in getSize() you are using sizeof(string) ... that is a constant regardless of how many
    characters are in the string.

    2) You can use seekp() for text files to get to specific records ... but the records need
    to be the same size.

    3) On non-Posix systems (read Windows), I would open the file in binary mode
    to prevent carriage return / linefeed translations.

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Random access file using fstream

    It's impossible to predict bytes in a file if every record doesn't have precisely the same size. Since you're using formatted output and a variable-length string, your records definitely don't all have the same size here.

  4. #4
    Join Date
    Feb 2009
    Posts
    326

    Re: Random access file using fstream

    Thanks a lot Philip Nicoletti and Lindley, I will try that as per your suggestions and get back on this.

    Sorry for the delayed response.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured