Thread: Binary post #xxx

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Binary post #xxx

    Ok so, having looked at the numerous other posts on problems with binary files (reading/writing) I cannot see what I am doing wrong.

    Code:
    void CFile::SaveFile()
    {
        std::ofstream fout;
        fout.open(filename.c_str(), std::ios::out | std::ios::binary);
    
        if( fout.is_open() )
        {
            for(int loop = 0; loop < MAX_DATA_SIZE; loop++)
                fout.write((char *)&DataList[loop], sizeof(CHighScore));
    
            fout.close();
        }
    }

    This part seemingly works just fine, MAX_DATA_SIZE is 10, CHighScore is 8 bytes, and I end up with a saved file of 80 bytes.

    But when I go to read it in, it opens the file, then shuts right down.
    I've also tried taking out the loop and just reading a single instance from the file, so I know I am just not understanding/missing something.


    Code:
    void CFile::LoadFile()
    {
        std::ifstream fin;
        fin.open(filename.c_str(), std::ios::in | std::ios::binary);
    
        if( fin.is_open() )
        {
            for(int loop = 0; loop < MAX_DATA_SIZE; loop++)
            {
                CHighScore temp;
                fin.read((char *)&temp, sizeof(CHighScore));
                DataList.push_back(temp);
            }
    
            fin.close();
        }
        else
            DefaultBuild();
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What is CHighScore? 8 bytes seems small for the size of a class.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Quote Originally Posted by rags_to_riches View Post
    What is CHighScore? 8 bytes seems small for the size of a class.
    It just holds a player's initials and score, a string and an int. Like I say, it seems to write the file just fine, and I know that it opens the file for reading (I oft use the window caption for debugging messages for myself). When it tries to read in data it closes down the window.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Incantrix
    It just holds a player's initials and score, a string and an int.
    Post the class definition. This is important because if by "string" you mean std::string, then what you are doing is wrong, because writing the bytes of a std::string to file does not work to store the contents of the std::string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Well, even if it were writing incorrect information, it should still be able to read in the 8 bytes, even if they gave nonsense, correct?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Incantrix
    Well, even if it were writing incorrect information, it should still be able to read in the 8 bytes, even if they gave nonsense, correct?
    True, but then this would result in undefined behaviour:
    Code:
    DataList.push_back(temp);
    so all bets are off, unless you can prove that it is this statement that causes the "shuts right down" thing to happen, on the first iteration of the loop:
    Code:
    fin.read((char *)&temp, sizeof(CHighScore));
    Why on earth are you resisting showing us the class definition?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read binary, change a value, write to binary
    By mammoth in forum C Programming
    Replies: 16
    Last Post: 05-12-2011, 10:56 AM
  2. Replies: 4
    Last Post: 03-06-2008, 03:38 PM
  3. Converting From Binary Tree to Threaded Binary Trees
    By elton_fan in forum C Programming
    Replies: 15
    Last Post: 11-08-2007, 11:41 PM
  4. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  5. Preludes old post about binary trees
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-17-2004, 08:00 AM