为什么我的文件是空的?

时间:2012-12-24 09:56:49

标签: c++ ifstream ofstream

为什么我的文件'database2'为空并且没有重命名的文件?在注释中声明问题的部分之后,文件'database2'变为0字节。文件'database2'也未重命名,文件'database'也未被删除。请帮帮我,这是我的代码的一部分:

int edit(){
    char KeyStroke;
    std::string strings;
    string name;
    string name1;
    std::string dummy;
    std::string strings2;
    std::string strings3;
    ifstream myfile;
    ofstream myfile1;
    bool found = false;

    myfile.open("database");
    cout << endl << "What is the name of the contact you wish to go in detail?" << endl;
    getline(cin, name);
    while ( !found && getline (myfile ,strings) ) {
        if (strings.find(name) != std::string::npos) {
            cout << endl << "Name:\t\t" << strings<<endl;
            std::getline( myfile, strings );
            cout << "Address:\t" << strings<<endl;
            std::getline( myfile, strings );
            cout << "Handphone:\t" << strings;
        }
    }

    start:
    cout << endl <<endl;
    cout << "What do you wish to edit?"<<endl;
    cout << "1) Name"<<endl;
    cout << "2) Address"<<endl;
    cout << "3) Handphone"<<endl;
    cout << "4) Nothing" << endl;
    myfile.close();


/*--Main part of this code is from here onwards--*/

    lol:
    myfile.open("database");
    myfile1.open("database2");
    KeyStroke = getch();
    switch(KeyStroke){
        case '1':
            cout << "What is the new name: ";
            getline(cin, name1);
            while ( !myfile.eof()) {
                getline (myfile ,strings);
                if (strings.find(name) != std::string::npos) {
                    myfile1 << name1 << endl;
                }
                else{
                    if(strings[0] == ' '){
                        continue;
                    }
                myfile1 << strings << endl;
                }
            }
        myfile1 << " ";
        myfile1.close();        /*Once the file closes here, the data written in earlier dissapears*/
        myfile.close();
        remove("database");
        pause1();
        rename("database2","database");
        goto start;

1 个答案:

答案 0 :(得分:3)

这是最新发生的事情:

你有一个“循环”,“开始”标签和结尾的转到。所以这将是循环的第一部分:

    cout << endl <<endl;
    cout << "What do you wish to edit?"<<endl;
    cout << "1) Name"<<endl;
    cout << "2) Address"<<endl;
    cout << "3) Handphone"<<endl;
    cout << "4) Nothing" << endl;
    myfile.close();


/*--Main part of this code is from here onwards--*/

    lol:
    myfile.open("database");
    myfile1.open("database2");
    cin.get (&KeyStroke,256);

在循环的第一部分中看到你打开/创建了两个名为“database”和“database2”的文件(注意,如果文件不存在,ofstream open方法会创建文件)然后等待用户输入。

让我们说用户按1更改名称,在案例1结尾处你有这个:

        myfile1 << " ";
        myfile1.close();        /*Once the file closes here, the data written in earlier dissapears*/
        myfile.close();
        remove("database");
//      pause1(); // undefined
        rename("database2","database");
        goto start;

你关闭文件,删除数据库,并将databese2重命名为数据库,这是按预期工作的,然后你回到带有goto的“循环”的开头,这再次对这部分代码进行了解释:< / p>

cout << endl <<endl;
cout << "What do you wish to edit?"<<endl;
cout << "1) Name"<<endl;
cout << "2) Address"<<endl;
cout << "3) Handphone"<<endl;
cout << "4) Nothing" << endl;
myfile.close();

lol:
myfile.open("database");
myfile1.open("database2");

您打开数据库文件,因为您将旧的database2重命名为数据库(database2现在不存在),所以它会创建一个新的(当然是空的)。

希望它有所帮助。