Thread: piping output input

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    piping output input

    Essentially, I'm trying to pipe the output of a text file, lets call it test.txt, into my C program, lets call it program.

    I assume I would evoke this by typing "cat test.txt | ./program".

    In the actual code, I would handle it like any command line operation ( utilizing argc/argv ). Is this logic correct? I don't have to do any pipe programming do I?

    Unfortunately my logic doesn't follow as it's not working. Any advice is appreciated.

    here is the code i have written - its pretty simple.

    Code:
    int main( int argc, char * argv[] )
    {
            int i;
    
            printf("%d\n", argc);
    
            for(i=1; i < argc; i++)
            {
                    printf("%s",argv[i]);
            }
    
            printf("\n");
    
            return 0;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You have to hope the shell you're running your program through doesn't parse your commands. You might type:

    ./foo < bar | baz

    But for command line arguments, foo might not get anything.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ File Input Output VS C file Input Output
    By forumuser in forum C++ Programming
    Replies: 1
    Last Post: 09-30-2009, 06:46 AM
  2. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  3. Replies: 5
    Last Post: 02-25-2008, 06:35 AM
  4. Bad output -- possible input buffer problems
    By jake123 in forum C Programming
    Replies: 8
    Last Post: 02-18-2008, 03:36 PM
  5. Using SSCANF input string output two variables
    By kevndale79 in forum C Programming
    Replies: 9
    Last Post: 10-02-2006, 02:57 PM