1

I am trying to write a data structure as such:

struct dataEntry
{
    std::list<int> listTiles;
    char* pData;
    int nSize;
} 

to a binary file.

I used ofstream to write to a binary file:

Write(char* fileName, const dataEntry& dataStruct)
{
    ofstream binFile("fileName, ios::out |      ios::binary | ios::trunc);
    if(binFile.open())
    {
        binFile.write((char*)&dataStruct,     sizeof(dataStruct));
        binFile.close();
    }
}

I used the same method to read back the binary file:

Read(char* fileName, const dataEntry& dataStruct)
    { 
        ifstream binFile("fileName, ios::in|      ios::binary );
        if(binFile.open())
        {
            binFile.read((char*)&dataStruct,         sizeof(dataStruct));
            binFile.close();
        }
    }

However, i cannot iterate through the list after i read the binary file. It gave me an exception saying that the "list iterator outside range".

2nd problem is that when i tried to read the binary file the 2nd time, the "pData" is not what I have entered.

int Main()
{
    char* name = "C:\\file.dat";
    char* buf = "ABCDEFG";
    dataEntry newData;
    newData.listTiles.push_back(1);
    newData.listTiles.push_back(2);
    newData.nSize = 5;
    newData.pData = buf;
    Write(name, newData);



    Read(name, newData);
    buf = newData.pData; // wrong value when read 2nd time
    newData.listTiles.remove(2); // crashed here       
} 
7
  • 2
    Open the file you wrote in an editor. Do you see anything in the file that looks like the data? I bet not. So given that, how did you expect to turn "junk" back into an object? The problem is that you should be saving the data that your class represents, not a pointer value and certainly not a bunch of bytes representing a list object. Google "object serialization". Commented Nov 25, 2015 at 3:56
  • So you are saying that i should be saving the list values itself(1,2) instead of the list object? Commented Nov 25, 2015 at 3:58
  • 1
    Yes, you save the data in some set, coherent manner so that you know how to "reverse the process" when recreating the object. Also, you will note that regardless of the number of items in your linked list, this: sizeof(dataStruct) never changes. you could have no items in the linked list, or a million items, the sizeof(dataStruct) stays the same. Again confirming that your method has no way of working correctly. Commented Nov 25, 2015 at 3:59
  • 1
    The pointer points to a string. Again, you save the data, not the pointer. A pointer is useless outside the scope of the running program. So you save (for example) 0xa6718bee. How does that represent a string when you want to read it back? That certainly doesn't look like "ABCDEFG" to me. Commented Nov 25, 2015 at 4:02
  • 2
    @Kai This may be relevant: boost.org/doc/libs/1_59_0/libs/serialization/doc/index.html Commented Nov 25, 2015 at 5:01

1 Answer 1

1

Check advanced c++ course on Udemy, the instructor said you cannot store pointers to binary files, because when you try to read them back the pointers (addresses) you used before writing them will not still be reserved for the list. So you have to save the data not the pointers

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.