Multiple Vectors Output/Input into binary file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kez
    New Member
    • Apr 2008
    • 1

    Multiple Vectors Output/Input into binary file?

    How do you read from a binary file into a structure that contains multiple vectors?

    For example

    struct studentRecords
    {
    string user;
    vector<string> friends;
    vector<string> enemies;
    }

    From what i gather if i wanted to output that data structure into a binary file i would need to create my own write function that iterates through the variables in the data structure and also each vector element

    Such as:
    void writeToFile()
    {
    //create fstream etc.
    myFile.write << user
    for(int i=0; i< friends.size(); i++)
    write << friends[i];

    for(int i=0; i< friends.size(); i++)
    write << enemies[i];
    }

    which seems fine but when i want to read the details of the binary files into a new data structure, how do it do this when vector1(friends ) and vector2(enemies ) might not be the same length?

    How do i find the size of a vector stored into a binary file?

    Thanks in advanced
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    As with all disc file reading, unless you know the format of the file, you can't read it.

    That is, when a file contains friends and enemies it is presupposed you know what's in the file in order to read it.

    If friends an emenies can be intemingled in the file, then you need to write out a packet that contains and indentifer (friend or enemy) and a length of the item floowed by the item itself. That way when you read, you can read the identifier and know what you are reading and how long it is.

    Otherwise, you use two files. One for friends and one for enemies.

    Comment

    • mad106
      New Member
      • Oct 2008
      • 2

      #3
      What if i have controls on a form that i wan to read and write


      Originally posted by weaknessforcats
      As with all disc file reading, unless you know the format of the file, you can't read it.

      That is, when a file contains friends and enemies it is presupposed you know what's in the file in order to read it.

      If friends an emenies can be intemingled in the file, then you need to write out a packet that contains and indentifer (friend or enemy) and a length of the item floowed by the item itself. That way when you read, you can read the identifier and know what you are reading and how long it is.

      Otherwise, you use two files. One for friends and one for enemies.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You will have to unload the data from the form and write in a format that you can read back and load another form of the same type. The form itself will not be in the disc file.

        Comment

        Working...