Thread: Saving vectors of structures problem

  1. #1
    Registered User
    Join Date
    Dec 2004

    Saving vectors of structures problem

    Hey all,
    I'm new on here, and I could do with any helpful info some kind soul could provide on a working method of saving a vector of structures to a binary file.
    I'm a CS student at a University in London (England). They've not taught us anything about this, which is most annoying.

    Could I beg of anyone versed in vectors and structures to knock up a sample bit of code that would save any vector containing structures to a binary file then load it back in?
    This program I've written so far for my assesment due in next Tuesday saves and loads - but just populates the loaded data full of garbage, and it only seems to save the data when it wants to.

    I'm so going to fail this subject at this rate... yet I aced my beginner's Java subjects.

  2. #2
    Registered User
    Join Date
    Aug 2003
    I am not going to give you any code so you can just rip that off! the rule is, you show code, we help.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Northern Virginia/Washington DC Metropolitan Area
    Do you know the basics of file I/O, opening, reading, writing using fstream objects? Are you versed in traversing/iterating through a vector? What type of data does the struct contain that you are storing in the vector? Special consideration needs to be given if the struct contains pointers to dynamically allocated/sized data and complicates matters a little.

    Personally I would overload the insertion << and extraction >> operators for the struct involved. Internally, these would probably need to call the streams read/get and write functions. Then I would just call the copy function to both write and read the entire vector to/from the file.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Well you could post the code to load and save - who knows, the reason why the load fails is because you messed up the save.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    This is a simple file I/O operation. No code, no help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM
  2. Problem with vectors and classes
    By applenerd in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 06:36 PM
  3. Structures problem
    By Krupux in forum C Programming
    Replies: 11
    Last Post: 08-12-2002, 02:28 PM
  4. problem with structures
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 01-28-2002, 02:12 PM