gpt4 book ai didi

c++ - 如何将控制台数据写入 C++ 中的文本文件?

转载 作者:行者123 更新时间:2023-11-27 22:30:24 26 4
gpt4 key购买 nike

我正在使用 C++ 开发一个文件共享应用程序。我想将控制台输出写入一个单独的文件,同时我也想在控制台中查看输出。任何人都可以帮助我...提前致谢。

最佳答案

我们开始...

#include <fstream>
using std::ofstream;
#include <iostream>
using std::cout;
using std::endl;

int main( int argc, char* argv[] )
{
ofstream file( "output.txt" ); // create output file stream to file output.txt
if( !file ) // check stream for error (check if it opened the file correctly)
cout << "error opening file for writing." << endl;

for( int i=0; i<argc; ++i ) // argc contains the number of arguments
{
file << argv[i] << endl; // argv contains the char arrays of commandline arguments
cout << argv[i] << endl;
}
file.close(); // always close a file stream when you're done with it.

return 0;
}

PS:好吧,你的问题读错了(控制台输出/输入混淆),但你仍然明白我的想法。

关于c++ - 如何将控制台数据写入 C++ 中的文本文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3289106/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com