fstream

Started by
2 comments, last by CTar 18 years ago
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.
Black Sky A Star Control 2/Elite like game
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.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.
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.

This topic is closed to new replies.

Advertisement