Like Tree1Likes
  • 1 Post By laserlight

Binary post #xxx

This is a discussion on Binary post #xxx within the C++ Programming forums, part of the General Programming Boards category; Ok so, having looked at the numerous other posts on problems with binary files (reading/writing) I cannot see what I ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    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
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,794
    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?
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 02: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, 10:41 PM
  4. Replies: 0
    Last Post: 11-04-2006, 10: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

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