打开/写入txt的基本程序随机崩溃/不打开

时间:2016-07-21 07:36:51

标签: c++ fstream

我知道这段代码非常非常严重优化,很可能很难理解,但我的问题是,我时不时地,大约每20次编译(我使用) Code :: Blocks IDE和MinGW编译器)文件不会打开并说访问被拒绝。值得注意的是,我只能通过Build :: amp; Run / Run of Code :: Blocks 永远从普通的exe引导文件 该程序打开一个名为notes.txt的文本文件,允许您在文本文件中写入,读取或删除信息。

编辑同样重要的是要注意,当我尝试从直接可执行文件启动文件时,我收到错误消息"您的计算机缺少libgcc_s_dw2-1.dll& #34; 此错误已在线修复,包括将所有.dll从我的编译器的bin文件夹移动到我的可执行文件的根目录。此修复程序在程序启动时起作用,但程序在之后立即崩溃。这让我相信它可能是编译器配置错误。一些澄清将非常感激。

这是编译器问题吗?这是内存泄漏吗? 虽然我不确定它是否真的是程序错误(我为这个非常糟糕的,非常糟糕的代码再次道歉)这是整个程序;

int main(){

    char a;
    char c;

    int b = 0;
    int ans = 0;
    int err = 0;
    int isopen = 0;
    int deleteerror = 0;
    int god = 0;

    string info;
    string docinsert;
    string writeto;

    fstream notes;
    notes.open("notes.txt");

    cout << "What would you like to do to the text in this document?"<<endl<<"Over[W]rite, [R]ead, [D]elete." << endl;
    cin >> a;
    while (b != 1){
        if (err != 0){
            cout << "Please retry." << endl;
            cin >> a;
        }
        switch(a){
            case 'W':
            case 'w':
            b = 1;
            ans = 1;
            break;

            case 'R':
            case 'r':
            b = 1;
            ans = 2;
            break;

            case 'D':
            case 'd':
            b = 1;
            ans = 4;
            break;

            default:
            err = 1;
            cout << "An error has occurred." << endl;
}
}

    if (notes.is_open()){
    isopen = 1;
        switch (ans){

        case 1:
                cout << "Please write what you wish to put within the document.\nThis will DIRECTLY overwrite anything previously written.\nIn order to avoid unwanted characters, delete before overwriting." << endl << endl;
                cin.clear();
                cin.sync();
                getline(cin, docinsert);
                cout << endl;
                notes << docinsert;
                break;

        case 2:
                cout << endl << "This is what is currently written within the document:" << endl;
                getline(notes, info);
                cout << info << endl;
                break;

        case 4:
                cout << endl << "This is what is currently written within the document:" << endl;
                getline(notes, info);
                cout << info << endl;
                cout << endl << "Please confirm you wish to delete all information in this document.\n[D]elete, [C]ancel.\n\n";
                cin >> c;
                while (god != 2){
                    if (god = 1){
                        cout << "Please repeat input to verify.\n";
                        cin >> c;
                    }
                if (c == 'D' || c == 'd'){
                    notes.close();
                    ofstream notes("notes.txt");
                    cout << "File wiped." << endl;
                    return 0;
                }
                else if(c == 'C' || c == 'c'){
                    cout << "\nNo information removed.";
                    return 0;
                }
                else{
                    cout << "An error has occured.\n";
                    god = 1;
                }
        }

        default:
                cout << "An error has occurred.\n";
                return 0;
        }
    }

    else if(isopen != 1){
        cout << endl << "An error with reading the file has occurred.\nIf this error persists, create a text document\nnamed 'notes' in the root directory of this program.\n";

    }
}

1 个答案:

答案 0 :(得分:-2)

fstream可以读写。因此,您必须在构造函数中告诉您将要使用的内容。 像那样:“fstream note(”notes.txt“,std :: ios :: out);或者fstream note(”notes.txt“,std :: ios :: in); 在指定“模式”之前,无法打开文件。