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
    Posts
    179

    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:
    Code:
    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")
                gfxServer.appendString(s)
    
    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
    Location
    Portugal
    Posts
    7,511
    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
    Location
    New York
    Posts
    1,465
    This windows specific code executes without error but does not actually redirect anything. It confuses me as to why.

    Code:
    	HANDLE hStdOut = ::GetStdHandle(STD_OUTPUT_HANDLE);
    
    	HANDLE hFile   = ::CreateFile
    		("C:\\abc.txt", GENERIC_WRITE, 
    		FILE_SHARE_READ, 0, CREATE_ALWAYS, 
    		FILE_ATTRIBUTE_NORMAL, 0);
    
    	if(hFile != INVALID_HANDLE_VALUE)
    	{
    
    		if(::SetStdHandle(STD_OUTPUT_HANDLE, hFile))
    		{
    			std::cout << "Testing\n";
    		}
    		else
    			std::cout << "Failed\n";
    
    
    		::SetStdHandle(STD_OUTPUT_HANDLE, hStdOut);
    		::CloseHandle(hFile);
    	}
    	else
    		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, 05:18 AM
  2. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  3. redirecting stdout
    By gregulator in forum C Programming
    Replies: 2
    Last Post: 04-22-2004, 10:07 AM
  4. redirecting stdout to a socket
    By Kinasz in forum Linux Programming
    Replies: 2
    Last Post: 03-25-2004, 07:01 AM
  5. redirecting stdout
    By Draco in forum C Programming
    Replies: 13
    Last Post: 10-11-2003, 12:56 AM

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