Bjorn Syla
Bjorn Syla

Reputation: 13

Delete specific line from .txt file (C++)

I am trying to scan through the content of a .txt file in order to compare it with a user's int input and once found the matching value, line where it is written to be removed. The file DOES NOT have only numerical values. Ex of what the file contains: " hello world 505 " " hello world 506 " etc.

So far my code is able to make the comparison(the lines do not have the same length) but I do not know how to move the file pointer to the line where said value is found.Any help?

void comparisoniD2 (int a) {

    int x[ countLinesOf() ];

    ifstream readFile;
    readFile.open("ClientList.txt");
    string str;
    for (int i = 0; i< countLinesOf();i++){
        while (getline(readFile, str)) {
            reverseStr2(str);
            str.erase(3);
            reverseStr2(str);
            stringstream geek(str);
            geek >> x[i];
            if( x[i]==a ){
                cout<<a<<" is equal to "<<x[i]<<endl;
                break;
            }
        }
    }

    readFile.close();
}

Upvotes: 1

Views: 122

Answers (1)

Evgeny
Evgeny

Reputation: 1072

If you want to do this in the same file it's slightly difficult.

For example you can open the same file twice: for reading strings and for saving strings. Your cycle would be: read string, save string (if it doesn't contain suitable number).

Another trouble is to cut file to lower size. You can use boost::filesystem or native api.

Upvotes: 1

Related Questions