C++ save/load

This is a discussion on C++ save/load within the C++ Programming forums, part of the General Programming Boards category; Hello I have data stored in variables in C++ I would like to be able to save that data to ...

  1. #1
    XjX
    XjX is offline
    Registered User
    Join Date
    Feb 2006
    Posts
    1

    C++ save/load

    Hello

    I have data stored in variables in C++ I would like to be able to save that data to a text file and then load it again when it is required. Do you know where I can find examples of this as I have looked on google but found nothing?

    Thank you ~J~

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    see the millions of posts here that discuss fstream c++ file i/o.

  3. #3
    Registered User Kurisu's Avatar
    Join Date
    Feb 2006
    Posts
    62
    ofstream a.k.a. output file stream.
    ifstream a.k.a. input file stream.

    ex:
    Code:
    int var1 = 5;
    
    ofstream dataFile;  // Declare variable.
    
    dataFile.open("data.txt");  // Open a text file.
    
    dataFile << var1;  // Output contents of var1 to data.txt.
    
    dataFile.close(); // Close file.
    
    int var2;
    
    ifstream dataFile;  // Declare input file stream.
    dataFile.open("Data.txt"); // Open the file.
    
    dataFile >> var2;  // Store first value of the data.txt into var2.
    
    dataFile.close();  // Close file stream.
    There are many more advanced things too like getline(); etc.. The above example works like cin & cout

  4. #4
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    I am also interesting in file saving is it also possible to do this with several structs?

    Is it also possible to create a text file for the data to be stored in rather than saving to a file that already exists?

    Thanks.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by 182
    I am also interesting in file saving is it also possible to do this with several structs?

    Is it also possible to create a text file for the data to be stored in rather than saving to a file that already exists?

    Thanks.
    Yes, but depending on the struct contents, you may need to consider writing/reading a certain way.
    "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

  6. #6
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Sorry for the newbie question but what do you mean by read and write in a certain way?

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by 182
    Sorry for the newbie question but what do you mean by read and write in a certain way?
    Code:
    struct person
    {
        string name;
    }
    If you write several of those to a file you could end up with "BobAmyMikeSarah" which could then get read as a single name "BobAmyMikeSarah" on the input side of things. You need to decide how to write the data such that it is clear on the read side of the program how to split up the input. This process is refered to as "serializing"/"serialization".
    Last edited by hk_mp5kpdw; 02-17-2006 at 11:40 AM.
    "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

  8. #8
    182
    182 is offline
    Registered User
    Join Date
    Oct 2005
    Posts
    133
    Ok thanks for that, I will have a go and writing it and post up what I get

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save/Load for ListBox
    By Brigs76 in forum C Programming
    Replies: 3
    Last Post: 06-16-2005, 07:04 AM
  2. Save/Load
    By RedRookie in forum Game Programming
    Replies: 8
    Last Post: 05-24-2005, 02:13 PM
  3. Save/Load function not working :(
    By Leeman_s in forum C++ Programming
    Replies: 6
    Last Post: 05-21-2003, 07:02 PM
  4. save/load problem
    By funkydude9 in forum C++ Programming
    Replies: 4
    Last Post: 11-08-2002, 02:22 PM
  5. Replies: 3
    Last Post: 06-04-2002, 06:11 PM

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