c++ Remove line from .txt file

10,579

This:

rename( cUser + ".txt", cUser + ".txt" );

is incorrect for two reasons:

  1. it is pointer arithmetic not string contatenation as cUser is a char[]
  2. even it was correct contatenation the old file name and new file name are the same

There is no reason to be using strcpy_s(), use operator+ for std::string:

const std::string oldFileName(user + ".txt");
const std::string newFileName(user + ".txt.new");

std::ofstream newFile(newFileName.c_str()); // c++11 allows std::string
if (newFile && newFile << line1)
{
    newFile.close();
    if (newFile)
    {
        if (0 == remove( oldFileName.c_str() ))
        {
            if (0 != rename( newFileName.c_str(), oldFileName.c_str() ))
            {
                // Handle rename failure.
            }
        }
        else
        {
            // Handle remove failure.
        }
    }
}

Remember to file.close() before attempting to remove() it.

Always check the result of IO operations, the code does not confirm that if file is opened or if any of the getline() attempts were successful:

ifstream file(user + ".txt");
if (file.is_open())
{
    string line1, line2;
    if (getline(file, line1) && getline(file, line2))
    {
        // Successfully read two lines.
    }
}
Share:
10,579
nlreturns
Author by

nlreturns

Updated on June 08, 2022

Comments

  • nlreturns
    nlreturns almost 2 years

    I'm trying to remove a line from my .txt file. The file contains all the information regarding the account.

    The line says "newAccount" and is made when creating the account. I use this so that I can boot up the tutorial when you first log in. After the tutorial I want to remove this line so that in next login you do not get the tutorial.

    Heres a snippet of the code: (doesn't work)

    void loginScreen(string user){
        system("CLS");
        cout << "Hello " + user << endl;
    
        ifstream file(user + ".txt");
        string line1;
        getline(file, line1);
        // Begin reading your stream here
        string line2;
        getline(file, line2);
    
        if(line2 == "newAccount"){
            cout << "Your account is new";
    
            char cUser[200];
    
            strcpy_s(cUser, user.c_str());
    
            std::ofstream newFile( user + ".txt.new" );
            //  Do output...!!!!!!!
            newFile << line1;
            newFile.close();
            if ( newFile ) {
                remove( cUser + ".txt" );
                rename( cUser + ".txt", cUser + ".txt" );
            } else {
                std::cerr << "Write error on output" << std::endl;
            }
        }
    
    }
    

    EDIT:

    I have edited my code to this and it still does not work:

    const string oldFileName(user + ".txt");
    const string newFileName(user + ".txt.new");
    
    std::ofstream newFile( user + ".txt.new" );
    //  Do output...!!!!!!!
    newFile << line1;
    newFile.close();
    
    
    if(line2 == "newAccount"){
        ofstream newFile(newFileName.c_str()); // c++11 allows std::string
        if (newFile){
            if (0 == remove( oldFileName.c_str() )){
                if (0 != rename( newFileName.c_str(), oldFileName.c_str() )){
                    // Handle rename failure.
                }
            }else{
                // Handle remove failure.
            }
        }