There is a problem with this that may be the cause:
1 2 3 4 5
while (myfile.good()){
getline (myfile,line);
if(line!=remove_line){
temp << line << endl;
}
You check the state of the stream before you attempt to read from it. Doing it that way means that you have no idea if the getline() function succeeded or not. What if the getline() function fails because the file ends? You still process the line regardless even though it may not have been read from the file.
A better way to do that loop would be:
1 2 3 4 5 6
while(getline(myfile, line)) // test that the return from getline() is a success
{
// now we know that the getline() function succeeded
if(line!=remove_line) {
temp << line << endl;
}
while (myfile.good()){
getline (myfile,line);
if(line!=remove_line){
temp << line << endl;
}
I get one extra line at the end of my file, every time.
If I use your code the behavior stops after the first itteration. That is, my starting file is
1 2 3 4 5 6
1
2
3
test line
4
5
and the output is
1 2 3 4 5 6
1
2
3
4
5
This is better because if I run the program again I do not get any additional blank lines (as my previous code did). However, I still get that extra blank line! Interestingly I get the same result if my initial file is
Well I suppose your code temp << line << endl; puts a new-line (endl) on the end of every line regardless if there was one in the file or not. If getline() ended by reading a new-line character then eof() should be clear otherwise eof() should be set because getline() looked for the new-line char but found the end of the file instead.
I think you are right. Would you mind explaining the syntax of the code you suggested? I think I understand what the code is doing but I am not completely comfortable with it. I would rather understand it than just paste it and have it work.