C++ 在C+中重定向+;
应该正常工作吗 我正试图这样做,但它不工作? 我将流传递给foo,使其存在于主管道中,以便foo结束时不会结束C++ 在C+中重定向+;,c++,redirect,stdout,cout,io-redirection,C++,Redirect,Stdout,Cout,Io Redirection,应该正常工作吗 我正试图这样做,但它不工作? 我将流传递给foo,使其存在于主管道中,以便foo结束时不会结束 streambuf *psbuf; ofstream filestr; void foo(streambuf*psbuf){ 流文件tr; filestr.open(“test.txt”); psbuf=filestr.rdbuf(); cout.rdbuf(psbuf); } int main(){ streambuf*psbuf foo(psbuf); 在我看来,你的代码应该可以
streambuf *psbuf;
ofstream filestr;
void foo(streambuf*psbuf){
流文件tr;
filestr.open(“test.txt”);
psbuf=filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main(){
streambuf*psbuf
foo(psbuf);
在我看来,你的代码应该可以工作,但是……你为什么不自己试试呢?你会看到所有的东西是否都是用test.txt编写的。在我看来,你的代码应该可以工作,但是……你为什么不自己试试呢?你会看到所有的东西是否都是用test.txt编写的。我怀疑现在已经编译并运行了你的代码,并且而且你有一个分割错误
之所以会出现这种情况,是因为您在foo()
中创建并打开了一个of stream
对象,然后在foo
末尾将其销毁(并关闭)。当您尝试在main()
中写入流时,您尝试访问一个不再存在的缓冲区
其中一个解决方法是使您的filestr
对象全局化。有很多更好的方法
编辑:以下是@MSalters建议的更好的解决方案:
void foo(streambuf *psbuf){
ofstream filestr;
filestr.open ("test.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main () {
streambuf *psbuf
foo(psbuf);
cout << "This is written to the file";
return 0;
}
#包括
#包括
类作用域\u cout\u重定向器
{
公众:
作用域重定向器(const std::string和filename)
:备份(std::cout.rdbuf())
,filestr(filename.c_str())
,sbuf_389;(filestr_389;.rdbuf())
{
标准:cout.rdbuf(sbuf_u2;);
}
~scoped_cout_重定向器()
{
标准::cout.rdbuf(备份);
}
私人:
作用域重定向器();
作用域重定向器(常量作用域重定向器和副本);
作用域重定向器和运算符=(常量作用域重定向器和分配);
std::streambuf*备份;
std::of流文件tr;
标准::streambuf*sbuf;
};
int main()
{
{
作用域重定向器file1(“file1.txt”);
std::cout我怀疑现在编译并运行您的代码时发现了一个分段错误
之所以会出现这种情况,是因为您在foo()
中创建并打开了一个of stream
对象,然后在foo
末尾将其销毁(并关闭)。当您尝试在main()
中写入流时,您尝试访问一个不再存在的缓冲区
其中一个解决方法是使您的filestr
对象全局化。有很多更好的方法
编辑:以下是@MSalters建议的更好的解决方案:
void foo(streambuf *psbuf){
ofstream filestr;
filestr.open ("test.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main () {
streambuf *psbuf
foo(psbuf);
cout << "This is written to the file";
return 0;
}
#包括
#包括
类作用域\u cout\u重定向器
{
公众:
作用域重定向器(const std::string和filename)
:备份(std::cout.rdbuf())
,filestr(filename.c_str())
,sbuf_389;(filestr_389;.rdbuf())
{
标准:cout.rdbuf(sbuf_u2;);
}
~scoped_cout_重定向器()
{
标准::cout.rdbuf(备份);
}
私人:
作用域重定向器();
作用域重定向器(常量作用域重定向器和副本);
作用域重定向器和运算符=(常量作用域重定向器和分配);
std::streambuf*备份;
std::of流文件tr;
标准::streambuf*sbuf;
};
int main()
{
{
作用域重定向器file1(“file1.txt”);
std::对我来说是不是没有,但你的问题仍然有效:-)对我来说是没有,但你的问题仍然有效:-)“有更好的”确实如此,但是如果没有指针就很难找到它们。这里有一个:阅读RAII。函数foo
中的大部分代码实际上应该移动到构造函数中,匹配的析构函数应该进行清理(例如将cout恢复到其旧状态).确实如此。@mati在将代码从foo()
中取出时,似乎故意忽略了将cout
恢复到原来的状态。RAII似乎太超出了问题的范围。“还有更好的。”确实如此,但是如果没有指针就很难找到它们。这里有一个:阅读RAII。函数foo
中的大部分代码实际上应该移动到构造函数中,匹配的析构函数应该进行清理(例如将cout恢复到其旧状态).确实如此。@mati在将代码从foo()
中取出时,似乎故意忽略了将cout
恢复到其以前的状态。RAII似乎超出了问题的范围。
#include <iostream>
#include <fstream>
class scoped_cout_redirector
{
public:
scoped_cout_redirector(const std::string& filename)
:backup_(std::cout.rdbuf())
,filestr_(filename.c_str())
,sbuf_(filestr_.rdbuf())
{
std::cout.rdbuf(sbuf_);
}
~scoped_cout_redirector()
{
std::cout.rdbuf(backup_);
}
private:
scoped_cout_redirector();
scoped_cout_redirector(const scoped_cout_redirector& copy);
scoped_cout_redirector& operator =(const scoped_cout_redirector& assign);
std::streambuf* backup_;
std::ofstream filestr_;
std::streambuf* sbuf_;
};
int main()
{
{
scoped_cout_redirector file1("file1.txt");
std::cout << "This is written to the first file." << std::endl;
}
std::cout << "This is written to stdout." << std::endl;
{
scoped_cout_redirector file2("file2.txt");
std::cout << "This is written to the second file." << std::endl;
}
return 0;
}