Sign in to follow this  

redirecting stdout to memory

Recommended Posts

davidkosenina    122
Helo Is it posible to redirect stdout to memory? Somehing like this: static char buffer[2048]; memset(buffer,0,2048); stdout->_base = buffer; // pointer to base char stdout->_bufsiz = 2048; // buffer size stdout->_charbuf = 0; stdout->_cnt = 2048; // buffer left stdout->_file = ??? // index to file 3; stdout->_flag = 128; // w+ stdout->_ptr = buffer + 1; // ponter to next char stdout->_tmpfname = 0; // filename As far I understand this stdout,stdin and stderr are 0,1,and 2 indices to file? So if I create new file it will have index 3. What hapens inside fopen?

Share this post

Link to post
Share on other sites
Evil Steve    2017
Only in the Windows implementation.
The internals of the CRT, and the structures used are OS and CRT version specific, meaning you're not supposed to play with them.
I did some digging around a while ago about this actually. What happens is there's a big array of FILE*s stored (256 or 128 I think). When you call fopen(), the CRT gets the next free FILE* from that array, sets up a buffer and a file HANDLE. The file HANDLE is the value returned from CreateFile(), which is used internally.
It just so happens that stdin, stdout and stderr are FILE*'s at index 0, 1 and 2 for compatability reasons (So you can pass stdout as a FILE* for instance).

I don't think you can redirect stdout to memory, but you may be able to redirect it to anything CreateFile() can open, like a network file or COM port. Although it'll only work on your OS, and possibly only on your compiler, which may be a consideration if you're distributing the code.

What exactly are you trying to do?

Share this post

Link to post
Share on other sites
jpetrie    13104
setbuf() or setvbuf() in C. These won't completely redirect the output but allow it to buffer up into a user-specified buffer instead of being written immediately or buffered into a default-allocated CRT buffer. This won't be a perfect solution.

If you are actually using C++ you can use a stringstream or provide a custom streambuf object (I think that boost::iostreams can ease that implementation though I must admit to never having used it).

Alternatively, the Win32 function SetStdHandle() can be used to set the standard output handle to anything that CreateFile() can open (same effect as Evil Steve's suggestion but you don't have to muck about in undocumented internals of the CRT).

If you are ultimately just trying to redirect where stdout goes (f.e.x to another file) you can use freopen(). If you are trying to redirect the output of a program you didn't write and don't have code for, somehow, you can simply use shell rediction (for example "dir > foo.txt" from console window on Windows).

If you can elaborate on why you're trying to do this there may be other options.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this