C ++删除功能不起作用

时间:2016-12-07 21:01:53

标签: c++ filestream

尝试编辑文本文件中的某些信息。我改变了我需要的东西,留下了我不想要的东西,并把它放在一个临时的文本文件中。程序完成后,我想删除旧的文本文件,并将临时文件重命名为旧文件。

在这种情况下,删除似乎不起作用......

else if (input == 2){
        cout<<"Work?"<<endl;
        string checkUser = username; //set checkUser var equal to username var
        string checkPass = password;
        ofstream outfile("pleasework.txt");
        ifstream infile("userinfo.txt"); //CREATING IFSTREAM
        string u, p, b;
        cout<<checkUser;
        while(infile>>u>>p>>b)
            {
           //cout<<"PLeae"<<endl;
           cout<<u<<' '<<p<<' '<<b<<endl;
            if(checkUser == u){
            cout<<"Current Balance: "<<balance<<endl;
            cout<<"Enter new balance: ";
            int newBalance;
            cin>>newBalance;
            outfile<<checkUser<<' '<<checkPass<<' '<<newBalance<<endl;
            //outfile<<username<<' '<<password<<' '<<newBalance<<endl;
            }else
            {
            outfile<<u<<' '<<p<<' '<<b<<endl;
            }//end else
        }//end while
            infile.close();
            cout<<"Infile closed"<<endl;
            outfile.close();
            cout<<"Outfile closed"<<endl;
            remove("userinfo.txt");
            cout<<"Were past remove!"<<endl;
            int result;
            char oldname[] = "pleasework.txt";
            char newname[] = "userinfo.txt";
            rename(oldname, newname);
            //result = rename (oldname, newname);
            result = (remove("userinfo.txt"));
            if(result == 0)
                puts("file was renamed");
            else
                perror("Error");
            return 0;

    }

我一直在&#34;错误:文件存在!&#34; 我错过了在某个地方关闭文件吗?

0 个答案:

没有答案