c++ Remove line from .txt file -


i'm trying remove line .txt file. file contains information regarding account.

the line says "newaccount" , made when creating account. use can boot tutorial when first log in. after tutorial want remove line in next login not tutorial.

heres snippet of 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 stream here     string line2;     getline(file, line2);      if(line2 == "newaccount"){         cout << "your account new";          char cuser[200];          strcpy_s(cuser, user.c_str());          std::ofstream newfile( user + ".txt.new" );         //  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 code , still not work:

const string oldfilename(user + ".txt"); const string newfilename(user + ".txt.new");  std::ofstream newfile( user + ".txt.new" ); //  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.         }     } 

this:

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

is incorrect 2 reasons:

  1. it pointer arithmetic not string contatenation cuser char[]
  2. even correct contatenation old file name , new file name same

there no reason using strcpy_s(), use operator+ 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 file.close() before attempting remove() it.

always check result of io operations, code not confirm if file opened or if of getline() attempts successful:

ifstream file(user + ".txt"); if (file.is_open()) {     string line1, line2;     if (getline(file, line1) && getline(file, line2))     {         // read 2 lines.     } } 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -