#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;
}
It seems to me that your code should work but ... Why don't you try yourself ? You will see if everything is written in test.txt or not.
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 withinfoo()
, which is then destroyed (and closed) at the end offoo
. When you attempt to write to the stream inmain()
, 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: