如何将控制台数据写入C++;? 我正在C++中开发一个文件共享应用程序。我想将控制台输出写入一个单独的文件,同时我还想在控制台中看到输出。我可以在C++中编程,但是我的建议是:创建新的类,它使用输入流(C++中的istrAM或SMTH),而不是它在STD中的每一个传入字节和文件。 我确信有一种方法可以通过前面提到的类来改变标准输出流。我记得,std.out是cout的某种属性。 而且,我在C++上花了1年多的时间,所以我说的所有的东西都是垃圾。 < p>我不在C++中编程,但我的建议是:创建新的类,它使用了输入流(C++中的istrAM或SMTH),而不是它在STD中的每一个传入字节和文件。 我确信有一种方法可以通过前面提到的类来改变标准输出流。我记得,std.out是cout的某种属性。 再一次,我在C++上花了1年多的时间,所以我说的所有的东西都是垃圾。 < P>我们走……/P> #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; } #包括 使用std::of流; #包括 使用std::cout; 使用std::endl; int main(int argc,char*argv[]) { of流文件(“output.txt”);//创建输出文件流到file output.txt if(!file)//检查流是否有错误(检查它是否正确打开了文件) 我们走吧 #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; } #包括 使用std::of流; #包括 使用std::cout; 使用std::endl; int main(int argc,char*argv[]) { of流文件(“output.txt”);//创建输出文件流到file output.txt if(!file)//检查流是否有错误(检查它是否正确打开了文件) cout

如何将控制台数据写入C++;? 我正在C++中开发一个文件共享应用程序。我想将控制台输出写入一个单独的文件,同时我还想在控制台中看到输出。我可以在C++中编程,但是我的建议是:创建新的类,它使用输入流(C++中的istrAM或SMTH),而不是它在STD中的每一个传入字节和文件。 我确信有一种方法可以通过前面提到的类来改变标准输出流。我记得,std.out是cout的某种属性。 而且,我在C++上花了1年多的时间,所以我说的所有的东西都是垃圾。 < p>我不在C++中编程,但我的建议是:创建新的类,它使用了输入流(C++中的istrAM或SMTH),而不是它在STD中的每一个传入字节和文件。 我确信有一种方法可以通过前面提到的类来改变标准输出流。我记得,std.out是cout的某种属性。 再一次,我在C++上花了1年多的时间,所以我说的所有的东西都是垃圾。 < P>我们走……/P> #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; } #包括 使用std::of流; #包括 使用std::cout; 使用std::endl; int main(int argc,char*argv[]) { of流文件(“output.txt”);//创建输出文件流到file output.txt if(!file)//检查流是否有错误(检查它是否正确打开了文件) 我们走吧 #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; } #包括 使用std::of流; #包括 使用std::cout; 使用std::endl; int main(int argc,char*argv[]) { of流文件(“output.txt”);//创建输出文件流到file output.txt if(!file)//检查流是否有错误(检查它是否正确打开了文件) cout,c++,visual-c++,cmd,console-application,C++,Visual C++,Cmd,Console Application,这个想法是创建std::streambuf的派生,它将数据输出到文件和cout 以下是代码(使用MSVC++2010进行测试,可以在任何编译器上运行): class StreambufDoubler:public std::streambuf{ 公众: StreambufDoubler(标准::streambuf*buf1,标准::streambuf*buf2): _buf1(buf1),_buf2(buf2),_缓冲器(128) { 断言(buf1&&buf2); setg(0,0,0); s

这个想法是创建std::streambuf的派生,它将数据输出到文件和cout

以下是代码(使用MSVC++2010进行测试,可以在任何编译器上运行):

class StreambufDoubler:public std::streambuf{
公众:
StreambufDoubler(标准::streambuf*buf1,标准::streambuf*buf2):
_buf1(buf1),_buf2(buf2),_缓冲器(128)
{
断言(buf1&&buf2);
setg(0,0,0);
setp(_buffer.data(),_buffer.data(),_buffer.data()+_buffer.size());
}
~StreambufDoubler(){
sync();
}
无效注入(const std::locale和loc){
_buf1->pubimbue(loc);
_buf2->pubimbue(loc);
}
std::streampos seekpos(std::streampos sp,std::ios_base::openmode which){
返回seekoff(sp,std::ios_base::cur,which);
}
std::streampos seekoff(std::streamoff,std::ios_base::seekdir方式,std::ios_base::openmode which){
if(哪个| std::ios|u base::in)
抛出(std::runtime_error(“无法使用此类读取数据”);
//要退哪一个?好问题
//无论如何,seekpos和seekoff都不应该被调用
_buf1->pubseekoff(关、路、哪个);
返回_buf2->pubseekoff(关、路、哪个);
}
整数溢出(intc){
int retValue=sync()?EOF:0;
sputc(c);
返回值;
}
int sync(){
_buf1->sputn(pbase(),pptr()-pbase());
_buf2->sputn(pbase(),pptr()-pbase());
setp(_buffer.data(),_buffer.data(),_buffer.data()+_buffer.size());
返回_buf1->pubsync()| _buf2->pubsync();
}
私人:
标准::streambuf*_buf1;
标准::streambuf*_buf2;
std::vector\u缓冲区;
};
int main(){
std::流myFile(“file.txt”);
StreambufDoubler倍频器(std::cout.rdbuf(),myFile.rdbuf());
标准:cout.rdbuf(&doubler);
//你的代码在这里
返回0;
}

但是请注意,更好的实现方法是使用模板、streambuf列表,而不是仅仅两个,等等。但我想让它尽可能简单。

想法是创建std::streambuf的派生,它将向文件和cout输出数据。然后创建一个实例并使用cout.rdbuf(…)

以下是代码(使用MSVC++2010进行测试,可以在任何编译器上运行):

class StreambufDoubler:public std::streambuf{
公众:
StreambufDoubler(标准::streambuf*buf1,标准::streambuf*buf2):
_buf1(buf1),_buf2(buf2),_缓冲器(128)
{
断言(buf1&&buf2);
setg(0,0,0);
setp(_buffer.data(),_buffer.data(),_buffer.data()+_buffer.size());
}
~StreambufDoubler(){
sync();
}
无效注入(const std::locale和loc){
_buf1->pubimbue(loc);
_buf2->pubimbue(loc);
}
std::streampos seekpos(std::streampos sp,std::ios_base::openmode which){
返回seekoff(sp,std::ios_base::cur,which);
}
std::streampos seekoff(std::streamoff,std::ios_base::seekdir方式,std::ios_base::openmode which){
if(哪个| std::ios|u base::in)
抛出(std::runtime_error(“无法使用此类读取数据”);
//要退哪一个?好问题
//无论如何,seekpos和seekoff都不应该被调用
_buf1->pubseekoff(关、路、哪个);
返回_buf2->pubseekoff(关、路、哪个);
}
整数溢出(intc){
int retValue=sync()?EOF:0;
sputc(c);
返回值;
}
int sync(){
_buf1->sputn(pbase(),pptr()-pbase());
_buf2->sputn(pbase(),pptr()-pbase());
setp(_buffer.data(),_buffer.data(),_buffer.data()+_buffer.size());
返回_buf1->pubsync()| _buf2->pubsync();
}
私人:
标准::streambuf*_buf1;
标准::streambuf*_buf2;
std::vector\u缓冲区;
};
int main(){
std::流myFile(“file.txt”);
StreambufDoubler倍频器(std::cout.rdbuf(),myFile.rdbuf());
标准:cout.rdbuf(&doubler);
//你的代码在这里
返回0;
}
但是请注意,更好的实现将使用模板,一个streambuf列表