I have a file where I hold some data in. When opening this file, I use ios::app, because I wan't to add things, without overwriting the file. But I want to chance the first line to (on the first line, the number of 'objects' in the file is stored). But I can't figure out how to get back to that line. Some solutions I tried:
And if, for example, the file looks like this:
9
AB
CD
And I add an object, will the endl be overwritten, because I write 10 (two characters) to the file? And if it is overwritten; is there an easy way to avoid this?
I don't see an arg provided to the ofstream constructor. Did the file open correctly? Provide a more complete example so that we can see your full attempt at solving the problem.
This line should get your stream set to the beginning but where is the rest of your code? out.seekp(0,ios::beg);
Little backgroundinformation: the file I want to edit is called Maze_allMaps.dat. This file looks like this:
Line 1: number of maps
Line 2: name first map
Line 3: filename where first map is stored
Line 4: name second map
etc.
First, we open the file and read the first line. Then we set the filename, and add the information to Maze_allMaps.dat. This is where I want to go to the beginning of the file; the number of maps needs to be increased. StringToInteger() and IntegerToString() do what there names say they do.
I solved it using another approach; now I simply read the whole file, and then overwrite it completly. But if someone can tell me why out.seekp(0,ios::beg) didn't bring me back to the beginning of the file, I would be thankful.