将 cout 写入文件,然后返回终端
Writing cout to a file and then back to terminal
我目前正在编写一个将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;
相关文章:
- 文字符号和字符串变量之间的串联然后返回常量字符*
- C - 将浮子转换为未签名的char数组,然后返回到浮点
- C 将字符串转换为char *,然后返回
- 通过将结构信息从二进制文件读取到数组中,然后返回到文件C
- 将光标更改为沙漏/等待/忙碌光标,然后返回Qt
- 将枚举传递上课,然后返回
- 打开一个 Shell 程序,向其传递参数,然后返回结果
- 从Istream中阅读,如何先读一个单词,然后读整行,然后返回
- 将短*/双*转换为整数*,然后返回并取消引用
- 将 cout 写入文件,然后返回终端
- 将字符串拆分为字符,然后返回字符串
- 将字符串转换为Unicode十六进制表示形式,然后返回C++
- 在函数中定义一个动态数组,然后返回数组元素。如何释放阵列的内存?
- 使用MPI临时接收数据,然后返回结果
- 将焦点设置到父窗口,然后返回到子窗口
- 删除它,然后返回创建的对象
- 用数字填充数组,然后返回指向该数组的指针
- 提升精神:如何将值对字符串解析为map<string,string>然后返回?
- C++跟踪一个项目一次,然后返回
- 计算平均值,然后返回数组中最接近平均值的元素