Thread: Random file IO problems in MinGW GCC

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    I live in Sherwood, Oregon. It's beautiful here!
    Posts
    7

    Question Random file IO problems in MinGW GCC

    Hi, everybody. It's been a while since my last post.

    I'm working with the MinGW GCC tool chain in Eclipse, which works perfectly about half the time. For some mysterious reason, when I do file read/write operations using seekg(), seekp(), tellg(), and tellp(), my code behaves very poorly.

    For example, take this code into consideration:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    int main()
    {
        using namespace std;
    
        ifstream inf("Sample.dat");
    
        // If we couldn't open the input file stream for reading
        if (!inf)
        {
            // Print an error and exit
            cerr << "Sample.dat could not be opened for reading!" << endl;
            exit(1);
        }
    
        string strData;
    
        inf.seekg(5, ios::beg);         // move to 5th character
    
        getline(inf, strData);	// Get the rest of the line and print it
        cout << strData << endl;
    
        inf.seekg(8, ios::cur);          // move 8 more bytes into file
    
        getline(inf, strData);	// Get rest of the line and print it
        cout << strData << endl;
    
        inf.seekg(-15, ios::end); 	// move 15 bytes before end of file
    
        getline(inf, strData);            // Get rest of the line and print it
        cout << strData << endl << endl;
    
        return 0;
    }
    Sample.dat contents:


    This is line 1
    This is line 2
    This is line 3
    This is line 4
    [Line 5 is a newline]


    This code isfrom a C++ tutorial on basic random file IO. It compiled and ran perfectly on the author's machine. I have the file, and the program compiles and runs on my system. The problem is that the file pointer seems to get messed up. The first line of output is correct for me. The second two are horribly wrong! This problem has been corrupting all of my advanced file IO programs.

    Here is the intended output:

    is line 1
    line 2
    This is line 4


    Here is what I get:

    is line 1
    e 2
    his is line 4


    Like I said, the file pointer gets a little confused about its intended position. What am I missing here?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What am I missing here?
    A couple of things
    One is that random seeking on a text file isn't supported. You can only be sure of
    - seeking to the beginning of the file
    - seeking to the end of the file
    - seeking to some result of a previous ftell()

    The other thing you're probably missing is that the author used a DOS file with \r\n endings, and you're using a UNIX file with only \n line endings. This will screw up your offset counts.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Location
    I live in Sherwood, Oregon. It's beautiful here!
    Posts
    7
    Interesting. I'll add a \r to the end of the file to see what it does.

    The file name I used is identical to the one used by the author. I am also using a pc with Windows 7. Sample.dat works for the author when he does random IO. Why wouldn't I be able to do random IO with it?

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    I live in Sherwood, Oregon. It's beautiful here!
    Posts
    7
    I checked the size in bytes of my file. It is the same as the author's. We both have \r\n endings, but his worked and mine didn't.

    Is there anything else?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Open it as a binary file.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM

Tags for this Thread