C++ 如何在C++;?

C++ 如何在C++;?,c++,string,file,stream,C++,String,File,Stream,请看看我做错了什么 //output-file stream ofstream file; file.open("output.txt", std::ios_base::app); //append bool isRunning = true; while (isRunning) { cout << "Please select an action:" << endl; cout << "ad

请看看我做错了什么

//output-file stream
    ofstream file;
    file.open("output.txt", std::ios_base::app); //append

    bool isRunning = true;

    while (isRunning) {
        cout << "Please select an action:" << endl;
        cout << "add - adding tasks to the list" << endl;
        cout << "del - deleting tasks to the list" << endl;
        cout << "list - show the list" << endl;
        cout << "x - to exit program" << endl;

        string input;
        cin >> input; 
        string addedTask;

        if (input == "add") {
            cout << "Please enter a task you like to add: " << endl;
            cin.ignore();
            if (std::getline(std::cin, addedTask)) {
                file << addedTask << "\n";
            }
            else {
                cout << "Failed to read line" << endl;
            }
        }
//输出文件流
流文件;
打开(“output.txt”,std::ios\u base::app)//追加
bool isRunning=true;
同时(正在运行){

你有没有试着换你的衣服

file << addedTask << "\n";

文件为什么要重复
.open()
文件?如果你多次打开文件,你需要多次关闭它。我已将该行移到外部,但仍然无法从外部移动到何处?是时候回答“否”了,这只会强制立即刷新。这不会影响文件中的最终数据。虽然如果OP试图在运行中观察文件,这可能是相关的,但没有说明。一般来说,您希望
'\n'
而不是
endl
。当主函数终止时,调用stream
file
close()
函数的
,该函数将刷新所有要写入文件的字符串。因此
endl;
不应该修复它。我们需要他的完整代码
file << addedTask << endl;