Thread: Alternative console output functions to printf()?

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    10

    Alternative console output functions to printf()?

    I'm trying to write a simple CL program that prints a line of text and repeats that line of text every time you press a key unless 'q' is pressed., at which point the program terminates.

    Easy, right? Sure. Took me about 3 minutes. Here's the problem:

    I'm using it as a listener program for netcat. I want my guys to be able to use this to debug port forwarding problems, etc.. The netcat tutorial says to use cmd.exe as a listener. It works great, but it's a huge security hole.

    The problem with my simple CL utility is that the output from printf() or puts() doesn't redirect the way I/O from cmd.exe, or ipconfig.exe does. It doesn't show up on the calling client's screen.

    Some MS utilities have redirectable output, some don't.

    Which output functions should I use to make this work?

    Thanks in advance ..

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Did you try using fprintf and using stderr or stdout as the FILE*?

    Note: One of the two has a good chance of working; I would think stderr.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    neither makes a difference. printf() is just fprintf(stdout, ...) anyway, right?

    puts() doesn't work either.


    cmd.exe, ipconfig.exe, and nslookup.exe DO redirect.

    time.exe, ping.exe, and msg.exe DO NOT redirect.

    So there is some difference in the I/O functions used in those utilities.

    Still stumped ...
    Last edited by jafadmin; 04-14-2014 at 09:01 PM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> ping.exe ... DO NOT redirect.
    What do you mean? Redirects Ok for me.

    C:\>ping google.com > out.txt

    C:\>type out.txt

    Pinging google.com [173.194.40.142] with 32 bytes of data:
    Reply from 173.194.40.142: bytes=32 time=105ms TTL=48
    Reply from 173.194.40.142: bytes=32 time=113ms TTL=48
    Reply from 173.194.40.142: bytes=32 time=107ms TTL=48
    Reply from 173.194.40.142: bytes=32 time=110ms TTL=48

    Ping statistics for 173.194.40.142:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
    Approximate round trip times in milli-seconds:
    Minimum = 105ms, Maximum = 113ms, Average = 108ms
    You could use popen() or this: Creating a Child Process with Redirected Input and Output (Windows)

    gg

  5. #5
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    Correct. Any utility using stdout can have the output piped to somewhere else. However, when using a listener program for netcat, stdout and stdin obviously aren't what's being redirected.

    I want to know what cmd.exe, ipconfig.exe, and nslookup ARE using for I/O. It is obviously not stdin/stdout.

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    How To Ask Questions The Smart Way

    Why did you NOT link to the tutorial?
    Why did you NOT post this in Windows or Networking sub-forum?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    Quote Originally Posted by stahta01 View Post
    How To Ask Questions The Smart Way

    Why did you NOT link to the tutorial?
    Why did you NOT post this in Windows or Networking sub-forum?

    Tim S.
    If you have no further suggestions, I understand. There is no need to try to derail the subject.

    This is about existing alternatives to standard I/O.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> It is obviously not stdin/stdout.
    Win32 processes have only 2 options for output - 1) WriteFile() to the standard output handle, which may be attached to a console, file, pipe, etc... 2) WriteConsoleOutput*() functions, which only go to an attached console and are not redirected.

    When using the CRT (printf etc) it just "does the right thing".

    gg

  9. #9
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    These aren't win32 processes. This is a DOS command line utility. The fact that it is on a windows PC is irrelevant. The exact same thing happens when compiling and running it on linux. STDOUT does not redirect.

    The difference is that on linux I can write a shell script that works. Not so on a MS machine.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by jafadmin View Post
    These aren't win32 processes. This is a DOS command line utility. The fact that it is on a windows PC is irrelevant.
    Unless you're using Turbo C or something equally archaic to create it, it is a win32 process. The fact that it runs on the command line is irrelevant.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  11. #11
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Some concrete examples that other can play with and examine might help.

    gg

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by jafadmin View Post
    If you have no further suggestions, I understand. There is no need to try to derail the subject.

    This is about existing alternatives to standard I/O.
    WHY DID YOU NOT LINK TO TUTORIAL?

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  13. #13
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    Quote Originally Posted by Codeplug View Post
    Some concrete examples that other can play with and examine might help.

    gg

    Sure. I think I figured part of it out though. For the output I had to use putc(stdout) and then fflush(stdout)

    Code:
    /*
     * Simple listener program for use with NetCat
     *  
     */
    #include <stdlib.h>
    #include <stdio.h>
    
    
    void PutStr(char *str)
    {
    	int i;
    	for(i=0; str[i]!='\0'; i++) 
    	{
    		putc((int)str[i], stdout);
    	}
    	fflush(stdout);
    }
    int main(int ac, char **av)
    {
        int ch, Counter=1, PortNum=atoi(av[1]);
        char str[80];
    
    
        sprintf(str, "\n\n\tRunning .........\n\n");
        PutStr(str);    
    
    
        do{        
    	sprintf(str, "\t\tYes, I am listening on port %d. [%d]\n", PortNum,Counter++);
            PutStr(str);
    	ch=getchar();
    		
        }while(ch!='q');
    
    
        return 0;
    }
    it is called like: >listener 4545

    it works the same on DOS or linux.

    Now I need to figure out how to get single character input without the user having to press <Enter> after each keypress ...

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Oh! You were talking about unbuffered IO. Since you're only calling fflush after you putc the entire string, you might as well just call printf/fprintf then call fflush. Also, you can look into the setvbuf function to make stdout unbuffered.

    Note that using setvbuf to unbuffer stdin will not fix the needing to press <enter> after each keypress. For that, you should look here: FAQ > How can I get input without having the user hit [Enter]? - Cprogramming.com.

  15. #15
    Registered User
    Join Date
    Apr 2014
    Posts
    10
    Quote Originally Posted by stahta01 View Post
    WHY DID YOU NOT LINK TO TUTORIAL?

    Tim S.
    It's on the boat and the boat isn't connected to shore power so I am unable to link. If it's really important to you I can send someone out in a dinghy.

    I hope this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-12-2012, 12:18 PM
  2. printf style output for C++
    By Mozza314 in forum C++ Programming
    Replies: 2
    Last Post: 11-09-2011, 12:31 AM
  3. printf output
    By shyguy24x7 in forum C Programming
    Replies: 3
    Last Post: 01-31-2009, 02:01 PM
  4. Printf Output Confusion
    By simpleid in forum C++ Programming
    Replies: 2
    Last Post: 11-16-2006, 11:36 AM
  5. Alternative forms of printf.
    By OOPboredom in forum C Programming
    Replies: 5
    Last Post: 05-07-2004, 01:21 PM