redirecting stdout

This is a discussion on redirecting stdout within the C++ Programming forums, part of the General Programming Boards category; Hey all, I'm working on a scripting console for my game engine and I've run into a problem getting everything ...

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002

    redirecting stdout

    Hey all,

    I'm working on a scripting console for my game engine and I've run into a problem getting everything from the real console to come up on my fake one. What I need to do is have some kind of buffered connection (preferably into a string or something similar) between stdout and my own acceptString function (ideally sending a string for every line that comes in from stdout). The solution I have to this right now works a little but not completely. Here's the python code that does it:
    class RedirectStdout:
        def __init__(self):
            self.oldstdout = sys.stdout
        def __del__(self):
            sys.stdout = self.oldstdout
        def write(self, s):
            if s.strip() != "":
                self.oldstdout.write(s + "\n")
    sys.stdout = RedirectStdout()
    The problem with this is that it only redirects some of the python output. What I need is something similar in C++ that will work at a lower level and hopefully catch all of the output from stdout.
    Illusion and reality become impartiality and confidence.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Look at rdbuf(). You can set the object associated with a stream.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.

    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    New York
    This windows specific code executes without error but does not actually redirect anything. It confuses me as to why.

    	HANDLE hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
    	HANDLE hFile   = ::CreateFile
    		("C:\\abc.txt", GENERIC_WRITE, 
    	if(hFile != INVALID_HANDLE_VALUE)
    		if(::SetStdHandle(STD_OUTPUT_HANDLE, hFile))
    			std::cout << "Testing\n";
    			std::cout << "Failed\n";
    		::SetStdHandle(STD_OUTPUT_HANDLE, hStdOut);
    		std::cout << "INVALID_HANDLE_VALUE: " << ::GetLastError() << std::endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting stdout to socket
    By rancor in forum C Programming
    Replies: 9
    Last Post: 10-18-2008, 06:18 AM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  3. redirecting stdout
    By gregulator in forum C Programming
    Replies: 2
    Last Post: 04-22-2004, 11:07 AM
  4. redirecting stdout to a socket
    By Kinasz in forum Linux Programming
    Replies: 2
    Last Post: 03-25-2004, 08:01 AM
  5. redirecting stdout
    By Draco in forum C Programming
    Replies: 13
    Last Post: 10-11-2003, 01:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21