将 cout 写入文件,然后返回终端

Writing cout to a file and then back to terminal

本文关键字:然后 返回 终端 文件 cout      更新时间:2023-10-16

我目前正在编写一个将cout写入文件的程序,直到用户按下控件D。之后,我想再次前往终端。这是我的代码示例

freopen(outputfile,"w",stdout);
for(;;)
{
    if(cin.fail())    //user pressed control-D
    {
    break;
    }
string s;
cin >> s;
cout << s << endl;
}
cout << "COMPLETE" << endl;

"完整"cout 仍在写入我的文件。我怎样才能停止它,以便任何不在循环中的 cout 都是正常的并打印回终端

提前致谢

已解决。我想要的是这个

ofstream tempfile;
tempfile.open ("temp.txt");
for(;;)
{
    if(cin.fail())
    {
    break;
    }
string s;
cin >> s;
tempfile << s << endl;  
}
tempfile.close();
cout << "COMPLETE" << endl;