close stdin pipe

This is a discussion on close stdin pipe within the Linux Programming forums, part of the Platform Specific Boards category; Hey gang! I'm not sure if this is bash specific, and I do want it to work as a multi-platform ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    close stdin pipe

    Hey gang!

    I'm not sure if this is bash specific, and I do want it to work as a multi-platform ansi compliant utility.

    The program takes input from stdin, so eg:

    Code:
    echo "some data" | theprogram
    However, once that data is piped in and saved in a buffer, some user input is needed:

    Code:
    #include <stdio.h>
    
    int main(void) {
    	char buffer[4096], ch;
    	int c = 0;
    
    /* get data from input pipe */
    	while ((ch = getchar())) {
    			if (feof(stdin) || c == 4096) break;
    			buffer[c++] = ch;
    	}
    
    /* data complete, now: */
            clearerr(stdin);
    	while ((ch = getchar())) {
    			printf("%d ",ch);
    	}
    
    	return 0;
    }
    The feof() happens, but after that there is a continuous and unstoppable stream of EOF characters:

    -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 ...etc.

    I can't off the top of my head think of a utility which accepts from stdin then switches to a TLI-ish control after the EOF, maybe this is why.

    I've tried fclose() on stdin, forking, etc but nothing helps. I can't use threads because it must be cross-platform.

    Is this just a no-go?
    Last edited by MK27; 02-16-2011 at 08:41 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Thanx -- reading from a stream opened on /dev/tty instead of stdin does work and should do on POSIX, I imagine there is no universal solution.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,648
    >> ... must be cross-platform.
    Ah - for Windows you'll want CONIN$
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
    Which can probably be fopen()'d as well.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pipe multiple programs using pipe() and c
    By HaitiBoy in forum C Programming
    Replies: 1
    Last Post: 12-07-2010, 04:19 PM
  2. Replies: 4
    Last Post: 10-14-2009, 04:44 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Pipe class.
    By eXeCuTeR in forum Linux Programming
    Replies: 8
    Last Post: 08-21-2008, 03:44 AM
  5. Ghost in the CD Drive
    By Natase in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 10-12-2001, 05:38 PM

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