Deleting text from file and adding again

Hi,

I couldnt understand why am i facing this problem.

for example.

[in my .txt file before delete]
a
b
c
d

[in my .txt file after deletion]
a
b
d

[in my .txt file after insertion]
a
b
d

c


how do i ensure that the newly inserted data will recorded on the empty line below "d" ???
Don't do while(!file.eof())
Galik,

That was sweet! I couldn't solve this problem for days. just one line from you and my problem is solved. THank you!
Hi teocl5

How did you deleted the line from your file?

Could you give me an example?

Thanks!
I am using the following code to delete from a file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void remove_line(string remove_line,string file)
{
//remove_line=test line
  string line;
  std::fstream myfile;
  std::fstream temp;
  temp.open("temp.txt",ios::out);
  myfile.open(file.c_str());
  if (!myfile.is_open()){
    cout << "\n\nCould not open file: "<<file<<endl;
    exit;
  }
  while (myfile.good()){
    getline (myfile,line);
    if(line!=remove_line){
      temp << line << endl;
    }
  }
  myfile.close();
  temp.close();
  remove(file.c_str());
  rename("temp.txt",file.c_str());
}


The file prior to deletion looks like this
1
2
3
4
5
6
1
2
3
test line
4
5


However, I have a problem. The file after deletion looks like this! Why are there extra lines at the end of my deletion?
1
2
3
4
5
6
7
1
2
3
4
5

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;
}
If I use

1
2
3
4
5
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

1
2
3
4
5
6
1
2
3
4
5


or

1
2
3
4
5
1
2
3
4
5


No matter what I end up with a single empty line.

Any ideas?
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.

Try:
 
temp << line << (is.eof() ? "" : "\n");
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.
I used the "ternary operator".
 
test ? A : B;

It basically means that if test is true, then use value A otherwise use value B.

So in the example I gave:
 
temp << (is.eof() ? "" : "\n"); // if is.eof() then output nothing "" otherwise output end-of-line "\n". 


I hope that makes sense.

EDIT:

Here is an equivalent:
1
2
3
temp << line;

if(!is.eof()) temp << '\n';
Last edited on
Topic archived. No new replies allowed.