Saving values...

This is a discussion on Saving values... within the C++ Programming forums, part of the General Programming Boards category; okay, Ill explain as best I can what I am doing. I am making a text game engine with an ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Question Saving values...

    okay, Ill explain as best I can what I am doing. I am making a text game engine with an editor. I have been coded for several hours now, and have come at a fork in the road. I know one way to save to a file via <fstream> is there a more easily used way. Since it has an editor I am going to have to be able to easily add data to these files. Yet the idea I am using now looks likes this...

    Character Save File--
    Code:
    0000 0000 000 000 00 00 00 00 00 00 00000 00000 1 NAME
    0000 0000 000 000 00 00 00 00 00 00 00000 00000 1 NAME
    0000 0000 000 000 00 00 00 00 00 00 00000 00000 1 NAME
    and so on and so forth up to 99
    Code for reading
    Code:
    for ( int x = 1; x != c; ++x ) {
    b_file >> preset.HPC[x] >> preset.HPB[x] >> preset.MPC[x] >> preset.MPB[x] >> preset.STC[x] >>
        preset.STB[x] >> preset.SPC[x] >> preset.SPB[x] >> preset.DFC[x] >> preset.DFB[x] >>preset.EXP[x] >> 
        preset.MON[x] >> preset.IID[x]>>preset.names[x];
    }
    This WORKS fine, but I cant seem to edit individual values in the save file. Modifying them is easy it is just saving them back to the file I cant figure out. So I figure I need to use a different approac. Thank you SO much for any help.
    Last edited by Salem; 07-10-2006 at 11:29 AM. Reason: code wrapping

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,789
    If your data is of a fixed format (fixed length) then it should be easy to open the file in read/write mode and seek to the appropriate value and modify it. If not, then you might do better to just read the whole file into memory (an STL container of some sort perhaps) and then modify the data and write it all back out to the file when you're done.
    "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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Text files are not really suitable for "in situ" modification, unless as hk_mp5kpdw notes, the records are fixed format.

    Rewriting the whole file is no biggie, and it works all the time without you having to expend a great deal of effort on it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    rewriting the whole file seem very inappropriate for just wanting to modify 1 value or maybe a line of data. is there a way to by chance add like a ID code to the beginning of a line and then search for the value then use something like getline to input the line into a string then modify string then replace the same line with the string? If not any ideas on a good way to code rewriting the entire file. I know how to but most of my code is very sloppy and slow. Just ideas would be good if not thanks for the help.

    EDIT: Umm, would I be better off using a GUI or API for this? Considering I am trying to make an editor I will need to be able to create variables/arrays during run-time.

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    make a map<string, vector<int>> perhaps?

  6. #6
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Raigne, I was having the same problem once, and wanted to make something which would just edit one specific part of my save file, but I decided against it (got bored looking), and just rewrote the whole file every time. Considering the files weren't too big, this didn't matter, so it worked out ok, and it worked out to be quite fast, so that was another relief.

    There were a few places where I had to rewrite it in my save file, so I made a function for both saving it and loading it. With all the error checking and whatnot required for FileIO, this helped a lot.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    ok how would I go about rewriting a 100 line file that has 8 values and one string on each line? I am quite good at reading files I just seem to have trouble writing stuff back out the right way. dont get me wrong I know the syntax, just it seems like my writing methods are a little whack. I also have a bad tendency to use for loops with almost all my file I/O

  8. #8
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Ok, you know your reading thing:

    Code:
    b_file >> preset.HPC[x] >> preset.HPB[x] >> preset.MPC[x] >> preset.MPB[x] >> preset.STC[x] >>
        preset.STB[x] >> preset.SPC[x] >> preset.SPB[x] >> preset.DFC[x] >> preset.DFB[x] >>preset.EXP[x] >> 
        preset.MON[x] >> preset.IID[x]>>preset.names[x];
    perhaps create a c_file (or whatever you want, I generally call them in and out), of an ofstream type, and pretty much do exactly the same. If I had a file with a [Name] [Age] and [Gender], I would do it like so:

    Code:
    ofstream out ( "MyFile.txt" );
    
    if ( out )
    {
    	// Name, Age and Gender variables previously defined etc
    
    	out<< Name << " " << Age << " " << Gender << '\n';
    }
    else
    {
    	cout<< "Error in FileIO!";
    }
    or something to that effect.

    EDIT: I didn't really answer the question did I? Well, that's a general way of writing data to a file. It's the exact same as reading, except the >> turn to << and the whitespace thing needs to be taken into account. Right, once you read in all the file info into variables (I'd say save them to an array), you can put the bit in the if () part into a loop like so:

    Code:
    for ( int i=0; i<NumThingsToBeSaved; i++ ) // You specify the variable NumThingsToBeSaved
    {
    	out<< Name[i] << " " << Age[i] << " " << Gender[i] << '\n';
    }
    Just change the Name, age and whatnot to your variables.
    Last edited by twomers; 07-10-2006 at 02:41 PM.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    yea that is basicly the same thing as what I did, just a little sloppier. Anyways thanks alot everyone

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  3. Computing Large Values
    By swbluto in forum C++ Programming
    Replies: 8
    Last Post: 04-07-2005, 03:04 AM
  4. Replies: 1
    Last Post: 02-03-2005, 02:33 AM
  5. ascii values for keys
    By acid45 in forum C Programming
    Replies: 2
    Last Post: 05-12-2003, 07:13 AM

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