Jump to content
  • Advertisement
Sign in to follow this  
ViperG

fstream

This topic is 4440 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it usually better to fully re-write a asci file from beginning to end than to go inside and change values? I was using fstream to read in values to get to a location, by using a while loop trying to find a certain word, then when I hit a match after I read the word it, I pass the get pointer to the put pointer then begin a << with my value. But for some reason this method wasnt working for me so I had to store the value in a int, then close the file and re-open. For some reason im not understanding, with fstream once I use the >> to read in, I can't use << to write something out, it just appears to fully ignore the file write. Even if my file.open is using ios:: in out and app. So just skipping ahead, I simply do a while << szword, then I stringcompare then get the location, save it to a int, then close the file and reopen and do my writes where I need. the other problem is. my file is like a game.ini file screenwidth 1024 screenheight 768 Bumpmaps 1 lights 0 etc etc Well my problem is lets say I locate the 7 as my start point, then I go file << 1024. well thats bad because after the 8 is the stored carrige return, so it writes over that and brings Bumpmaps to the same line with some funky char before it. so it looks something like screenwidth 1024 screenheight 1024 □ Bumpmaps 1 BAD BAD! I mean my game will still read it as long as spaces are between everything since im using the << operaters with fstream. So problem 1 is im unable to write if I read first, my solution is to reopen the file. second problem is how do I "insert" text or not flood into the next line? when im writing? Especially when my current word, happens to be too long than the current line in the file itself. or if anyone has any other good ideas for updating 1 or 2 values in a ini file.

Share this post


Link to post
Share on other sites
Advertisement
I'm pretty sure you can only overwrite/append data, not insert/remove data. From the point of view of the file system where files are stored in fixed sized blocks, inserting/removing wouldn't be a very pretty sight.

Share this post


Link to post
Share on other sites
I'm with smart_idiot on that one. If you don't want to have to process the whole file the only other way would be to read the whole thing into a char array with fread, insert into that (with memmove etc?) and write the array back out with fwrite, although there may be better options for this sort of memory management in the STL or something.

Share this post


Link to post
Share on other sites
You can't just change text in the middle of a file, as EasilyConfused and smart_idiot have pointed out. What most likely happens is that a newline is "/n/r" so when you write 1024 you overwrites 768 and the /n so all the reader will see is /r which doesn't mean much alone.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!