Writing contents of an std::vector to a binary file! ;o)

This is a discussion on Writing contents of an std::vector to a binary file! ;o) within the C++ Programming forums, part of the General Programming Boards category; Hi, I have already written the contents of a regular good old array to a bin file many times. The ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    87

    Post Writing contents of an std::vector to a binary file! ;o)

    Hi,

    I have already written the contents of a regular good old array to a bin file many times. The program I've made needs some resizing to take place in it though - std::vector needed. But I want to write the contents of that std::vector out to a binary file. Up to now it's used this command for a regular array:

    Code:
    std::ofstream writeFile;
    writeFile.open("Mesh.bin", std::ios::out | std::ios::binary);
    writeFile.write((char*)newVertexArray, VERTEXARRAYSIZE * sizeof(float));
    But I know that happy simplicity will abruptly end when trying to do it with an std::vector. I haven't a clue how to do this. Has *anyone* got any ideas.

    Thanks!

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    19
    Vectors aren't contiguous blocks of memory, so you can't simply write the block of memory to a file. Instead, you'll have to manually write each element (assuming each element is itself a contiguous block of memory, i.e. no pointers). You should be able to do something clever with std::copy and an OutputIterator, if that's closer to what you were looking for.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    Perhaps this is a defect in the C++ standard, as std::string has the same problem, but the intent is that vector is contiguous. If your implementation does not store a vector in contiguous memory, it should not be considered as conforming to the Standard.

    As the fix to store std::strings in contiguous memory is vector<char>, it had better be guaranteed to be contiguous.

    As it happens, no implementation I know of fails in this regard.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by ctrl_freak View Post
    Vectors aren't contiguous blocks of memory, so you can't simply write the block of memory to a file.
    Vectors contents are meant to be guaranteed by the standard to be contiguous in memory. The problem here is that you cant cast the vector itself that way. You instead need to get the address of the first item.

    The following is a legitimate way of doing it. Also note the other changes I made:
    Code:
    std::ofstream writeFile;
    writeFile.open("Mesh.bin", std::ios::out | std::ios::binary);
    if (!newVertexArray.empty())
        writeFile.write(&newVertexArray[0], newVertexArray.size() * sizeof(float));
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    Well I think these replies are great! Somone else taking so much time to help with no monetary reward. Thank you so much this is enough information for me to go forward from here (I hope!)

  6. #6
    Registered User
    Join Date
    Jan 2010
    Posts
    87
    Excellent stuff this worked:

    Code:
    std::ofstream writeFile;
    writeFile.open("Mesh.bin", std::ios::out | std::ios::binary);
    if (!newVertexArray.empty())
        writeFile.write((char*)&newVertexArray[0], newVertexArray.size() * sizeof(float));
    It still demanded the first address be cast to a char*. Thanks alot this will really help me write better code.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,166
    Quote Originally Posted by shrink_tubing
    It still demanded the first address be cast to a char*.
    Yes, though you might want to use a C++-style cast to make the cast more explicit:
    Code:
    std::ofstream writeFile;
    writeFile.open("Mesh.bin", std::ios::out | std::ios::binary);
    if (!newVertexArray.empty())
        writeFile.write(reinterpret_cast<char*>(&newVertexArray[0]),
            newVertexArray.size() * sizeof(newVertexArray[0]));
    I have also chosen to change the sizeof expression to one that does not need to be changed should the value type of newVertexArray change.
    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

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    87

    Thumbs up

    Thank you so much madam that's great
    Last edited by shrink_tubing; 08-22-2010 at 08:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems installing Mingw5.1.4
    By BlackOps in forum C Programming
    Replies: 2
    Last Post: 07-26-2009, 04:28 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Binary Tree - Reading From and Writing to a File
    By Ctank02 in forum C++ Programming
    Replies: 2
    Last Post: 03-15-2008, 10:22 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  5. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM

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