c++ Random Access File I/O

Discussion in 'Software' started by Senlis, Feb 9, 2009.

  1. Senlis

    Senlis Staff Sergeant

    I am having problems with random file access. I have made a test program of what I want to implement in a larger program. Simply put, I have three function: createRandomAccessFile, writeToRandomAccessFile, and readFromRandomAccessFile. I will start with creating the random access file. Everything is fine then. However, when I start writing to the random access file, the file I made earlier looses all of it's information. Every time I call writeToRandomAccessFile, the file test.dat goes blank. This happens right after the file is opened and before I write anything.

    Could you look at it and tell me what I am doing wrong. The cout statements are for debug purposes only. When you run the program, typing 'a' then enter call createRandomAccessFile, 'b' calls the write function and 'c' call the read function. 'x' exits.
     

    Attached Files:

  2. Senlis

    Senlis Staff Sergeant

    I did a little more towards finding out what the problem is. This code section from my program will shed some light.

    //-------------determine status of file before opening output file----------------
    ifstream inRecord("test.dat", ios::in);
    if (!inRecord)
    {
    cerr << "File could not be opened." << endl;
    exit(1);
    }

    inRecord.seekg(0, ios::end);
    cout << "File size before opening for output: " << inRecord.tellg() << endl;
    inRecord.close();
    //--------------------------------------------------------------------------------

    fstream out("test.dat", ios::binary | ios::eek:ut);
    if (!out)
    {
    cerr << "File could not be opened." << endl;
    exit(1);
    }

    out.seekp(0, ios::end);
    cout << "File has been opened, prelimary size:" << out.tellp() << endl;

    This is the output from this part of the code:

    File size before opening for output: 800
    File has been opened, prelimary size:0

    Simply, whenever the program hits the line starting with fstream out, the file test.bat is cleared. I have tried ofstream out along with simply ios::binary instead of ios::binary | ios::eek:ut. Does anyone know a way I can open the ofstream without clearing the file?
     
  3. Senlis

    Senlis Staff Sergeant

    I figured out the easiest way to open the file for writing without clearing it is to

    fstream out("test.dat", ios::in | ios::eek:ut);

    I suppose that since I am opening for input, it figures out I don't want to clear it. I am still looking for a better way to open the file to be more secure (coding accidents won't cause it to write when I don't want it to);
     
  4. t.c.durai

    t.c.durai Private E-2

    hi friend how can i open .sig format file please help me
     
  5. Senlis

    Senlis Staff Sergeant

    Welcome to Majorgeeks forums Durai. I will suggest that when seeking help in the future, you are better of creating a new thread for the problem instead of posting it in an unrelated thread, like this one. The reason is that it is more likely you will get helped.

    Anyway, I googled .sig extension and have gotten results saying it could be a signature file from an email or anti-virus signatures for a-squared antivirus. If I know where you got the file, I may be able to tell you more.
     
  6. PC-XT

    PC-XT Master Sergeant

    Are you trying to open for append? I think there is an ios::app for that.
     
  7. Senlis

    Senlis Staff Sergeant

    I tried ios::app. However this is what would happen.

    after creating my random access file initialy: it would be 800KB. This filesize should never change.
    I would open the file to append an 8KB record. I would check the size again and it would be 808KB.

    So apparently the ios::app didn't do what I wanted. However, opening the file with ios::in and ios::eek:ut fixed my problem.
     
  8. PC-XT

    PC-XT Master Sergeant

    I understand. I had the same problem in PHP. I tried seeking after opening in append mode, but it still added the record to the end of the file. I imagine you could make a wrapper class to give the control that you have to give up by using in and out, but I haven't done this.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds