Removing a specific text from a textfile

Problem Solved Thanks!!
Last edited on
Well I can't off-hand see anything wrong with your logic, but your file reading loop should not test for eof(). The reason being that EOF is not flagged until after the program tries to read past the end of file. So if your getline() fails you are going to be using an invalid entry.

The recommended way to do this is:
1
2
3
4
5
6
7
	/*Read File*/
	while(std::getline(iFile, temp))  // Make getline() the condition
	{
		// Now you know that the line was successfully read
		// and that valid data is going into your vector
		file.push_back(temp);
	}


Dude thx alot it works!! Now the program is running well!!
Urm sorry one last question..

I entered both input of the same name into the program.
Lets call the first input detail "a".
Second input "a".

However when I want to remove one of the detail, let's say the first detail.
They delete both of them since they share the same name.
Is there anyway to select one of the details so I can removed it?
Actually looking more closely at your code it seems a little inefficient. In your loop at line 22 to search for a detail to delete, you write the new file out to disk every time you delete something. There is no need to do that until *after* you have found and removed your elements.

Simply moving the loop closing brace at line 38 to line 30 should fix that.

Then it is simply a matter of breaking out of the loop after you delete the first element:
1
2
3
4
5
6
if(file[i].substr(0, des.length()) == des)
{
	file.erase(file.begin() + i);
	cout << "Detail Deleted" << endl;
	break; // exit the searching loop
}
Last edited on
Thx Galik!!

When I change to break, it cannot delete the line.
But when I change to i=0 it works..

However,
After I added some input inside, it return back to how it was.

bump
Last edited on
Did you move the end of the loop like I mentioned? That may make all the difference. I imagined your reInc() function to look more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void reInc(){

    vector<string> file;
    string temp;
    string des;

    ifstream iFile("Income.txt");

    /*Read File*/
    while(!iFile.eof()){

        getline(iFile,temp);
        file.push_back(temp);

    }

    iFile.close();
    /*User type in the details to delete*/
    cout << "Enter DETAILS to delete: ";
    getline(cin,des);

    for(int i = 0; i < (int)file.size(); i++){


        if(file[i].substr(0, des.length()) == des){
            file.erase(file.begin() + i);
            cout << "Detail Deleted" << endl;
            break; // end loop after deleting first one found
        }

    } // end loop here

    // Do this outside of loop

    /*Update new list*/
    ofstream oFile("Income.txt", ios::out | ios::trunc);

    for(vector<string>::const_iterator i = file.begin(); i != file.end(); i++){
        oFile << *i <<endl;
    }
    oFile.close();
}

I recommend doing the same for your other functions. Update the data in your vector completely before writing it to the file on disk. Do all the writing to file on disk outside the loops that update the data.
Last edited on
Topic archived. No new replies allowed.