Hi
I'm dealing with a text file which I'm reading and writing to, at some points I need to increase the characters on a line and in doing so shift everything along abit. The problem is it seems to read characters at the end of the file which arent there.
The code I'm using for this function is:
note:spaceString() returns a blank string with the length of its argument.
with outFile being a fstream object with a valid file open. It reads everthing ok apart from "ÍÍÍÍÍÍ" extra at the end which I've been unable to locate where they appear from.
Many thanks
John
Stuff dumped from the end of a stream is usually just junk data sitting in memory I doubt it's anything in your file.
Honestly as cool as what you are trying to do is, I've tried it before when I was first starting to use the fstream stuff and I just ran in circles. To accomplish what you are trying to do you may be better off using the string library, it allows you to do stuff like read in a line (using the fstream lib) then append or preppend data then you can use the fstream to write it back into the file.
Working on a file one line at a time like this can be done but it feels a little silly. Try reading the whole file into memory, an array would be fine but a vector is better, then you can manipulate it anyway you want before over writting the previous version of the file with the new data you have, if the data is important enough to warrent some level of protection I suggest reading it all in then outputting a temp file that won't be destroyed unless your process terminates with a 0.
Thanks for the help, I would prefer to do it by reading and writing to a file as I have already coded many functions based on this approach however if I can't find a fix then I'll do what you said (it sounds easyier as well). However wont I run into the same problem if I read() the whole file and put it into a string e.g. I would still have the wierd redundant charaters?
Thanks
John