在C ++中重定向 [英] Redirecting in C++
问题描述
#include< iostream>
#include< fstream>
using namespace std;
void foo(){
streambuf * psbuf;
ofstream filestr;
filestr.open(test.txt);
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main(){
foo();
cout<< 这是写入文件;
return 0;
}
cout是否写入给定文件?
如果没有,是否有一种方法可以不将变量发送到foo,例如 new
?
更新:
不使用使用类或使用全局的解决方案,所以plz可以一些
给我使用新的解决方案。也将主从传递到foo
streambuf * psbuf;
ofstream filestr;
应该能正常工作吗?
试图做到这一点,但它不工作?
我将流传递给foo,所以它存在于main中,所以当foo完成时不会结束。
void foo (streambuf * psbuf){
ofstream filestr;
filestr.open(test.txt);
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main(){
streambuf * psbuf
foo(psbuf);
cout<< 这是写入文件;
return 0;
}
你得到这个是因为你创建并打开了一个 ofstream $ c $在
foo()
中的 foo
结尾处销毁当您尝试在 main()
中写入流时,您尝试访问不再存在的缓冲区。
一个解决方法是使您的 filestr
对象全局。
编辑:这里是一个更好的解决方案,由@MSalters建议:
#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<< 这是写入第一个文件。 << std :: endl;
}
std :: cout< 这是写到stdout。 << std :: endl;
{
scoped_cout_redirector file2(file2.txt);
std :: cout<< 这是写入第二个文件。 << std :: endl;
}
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
void foo(){
streambuf *psbuf;
ofstream filestr;
filestr.open ("test.txt");
psbuf = filestr.rdbuf();
cout.rdbuf(psbuf);
}
int main () {
foo();
cout << "This is written to the file";
return 0;
}
Does cout write to the given file?
If not, is there a way to do it without sending the variables to foo, like new
?
update :
I can't use a solution that uses class or uses global so plz can some give me solution that use new. Also passing the from main to foo
streambuf *psbuf;
ofstream filestr;
should work right?
I am trying to do this but its not working? I pass the stream to foo so it exist in the main so it wont end when foo finish.
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;
}
I suspect that by now compiled and run your code and found that you get a segmentation fault.
You are getting this because you create and open an ofstream
object within foo()
, which is then destroyed (and closed) at the end of foo
. When you attempt to write to the stream in main()
, you attempt to access a buffer which no longer exists.
One workaround to this is to make your filestr
object global. There are plenty of better ones!
Edit: Here is a better solution as suggested by @MSalters:
#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;
}
这篇关于在C ++中重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!