文本文件问题 (Visual C++)

Text file Issues (Visual C++)

提问人:Paul Pulcini 提问时间:11/17/2018 更新时间:11/17/2018 访问量:40

问:

我正在尝试使程序提示用户输入文件的名称,然后提示他们输入要删除的单词。 有没有办法在不使 main 之外的任何功能的情况下做到这一点? (提前致谢! 这是我到目前为止的代码:

ofstream outFile;
string in;
char in2[800];
char fileName = ' ';
while (in != "XXXX") {
    cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
    cin >> in;

    for (int i = 0; i < in.length(); i++)
    {
        fileName += in[i];
    }
    outFile.open(in + ".txt");
    if (outFile.fail())
    {
        outFile << "Can't do it.";
    }
    else
    {
        cout << "What do you want in it?\n";
        cin >> in2;
        cin.getline(in2, sizeof(in2));
        outFile << in2;
    }
    outFile.close();
    ifstream reader;
    string oneLine;
    reader.open(in + ".txt");
    if (reader.fail())
    {
        cout << "Could not read file.\n";
        return 0;
    }
    else
    {
        reader.clear();
        reader.seekg(60L, ios::beg);
        getline(reader, oneLine);
        cout << "Is there something you'd like to remove from " << in << ".txt?\n";
        cin >> in2;
        ofstream write("tmp.txt");
        while (getline(reader, oneLine)) {
            if (oneLine != in2)
                write << oneLine << endl;
        }
        reader.close();
        write.close();
        remove(fileName + ".txt");
        rename("tmp.txt", fileName + ".txt");
        return 0;
    }
    return 0;
}
C 可视化-C++ Fstream IOstream

评论

0赞 user4581301 11/17/2018
道歉。读错。 是数组,而不是.in2charin
0赞 user4581301 11/17/2018
你有什么理由不想要函数吗?从职责分工和代码可读性的角度来看,它们几乎总是更可取。
1赞 Hristijan Gjorshevski 11/17/2018
逐行检查代码,了解每个语句的作用。fileName += in[i] 不会做你可能做的事,除了它。
0赞 johnathan 11/17/2018
@Hristijan Gjorshevski 也没有他的 out file <<“做不到”;在该特定代码块中。

答:

0赞 Barmak Shemirani 11/17/2018 #1
while (in != "XXXX") {
    cout << "What do you want to name the file? Enter a name or enter XXXX to quit.\n";
    ...
}

如果你进入循环,就不会退出。它将尝试处理其余的代码。XXXX

char fileName = ' ';声明单个字符。您需要一个字符数组。更好的是,将其声明为std::string fileName;

remove(fileName + ".txt");不会编译,因为是 C 函数。C 不支持类,也不知道如何处理 .你想提供一个 C 字符串,你可以用 以下是一些建议:removestd::stringfileName.c_str()

int main()
{
    ofstream outFile;
    string fileName;
    string text;
    for(;;)
    {
        cout << "What do you want to name the file?\n";
        cout << "Enter a name or enter 999 to quit.\n";
        cin >> fileName;

        if(fileName == "999")
            break;

        fileName += ".txt";
        outFile.open(fileName);
        if(outFile.fail())
        {
            cout << "Can't do it.";
            continue;
        }
        else
        {
            for(;;)
            {
                cout << "What do you want in it?\n";
                cout << "Enter a name or enter 999 to stop.\n";
                cin >> text;
                if(text == "999")
                    break;
                outFile << text << endl;
            }
        }
        outFile.close();

        ifstream reader;
        reader.open(fileName);
        if(reader.fail())
        {
            cout << "Could not read file.\n";
        }
        else
        {
            cout << "Is there something you'd like to remove from " << fileName << "?\n";
            cin >> text;

            string oneLine;
            ofstream write("tmp.txt");
            while(getline(reader, oneLine)) 
            {
                if(oneLine != text)
                    write << oneLine << endl;
            }
            reader.close();
            write.close();

            remove(fileName.c_str());
            rename("tmp.txt", fileName.c_str());
            break;
        }
    }
}

评论

0赞 Paul Pulcini 11/19/2018
谢谢Barmak!这完全符合我的意愿。