pipe with program arguments does not work?

Discussion in 'C Programming' started by jan.dostrasil@seznam.cz, Oct 4, 2005.

  1. Guest

    Hello, I'm new to c - so sorry if this is off topic here, please advice
    at least where should i ask... ;)

    I have a program in c which is working fine, it reads everything from
    pipe, modifies output data a bit and writes to stdout.

    When it is executed without parameters, program works fine. But what if
    I'd like to pass some extra configuration parameters to my program?

    1. c:\> pipetest < file_in > file_out // this works
    2. c:\> pipetest /parameter < file_in // also works, writes
    file_in and parameters to stdout
    3. c:\> pipetest /parameter < file_in > file_out // nothing happens !?

    Thanks anyone for kind advice and a nice day to all,
    Y.

    // pitest.c
    #include <stdio.h>
    #include <fcntl.h>
    #include <io.h>

    int main(int argc,char *argv[]) {
    int ch,i;
    for (i = 1; i < argc; i++) {
    printf("%s\n",argv);
    }
    setmode(fileno(stdin), O_BINARY);
    setmode(fileno(stdout), O_BINARY);
    ch=getc(stdin);
    while (!feof(stdin)) {
    putc(ch,stdout);
    }
    }
    // -------
    , Oct 4, 2005
    #1
    1. Advertising

  2. Barry Guest

    <> wrote in message news:...
    > Hello, I'm new to c - so sorry if this is off topic here, please advice
    > at least where should i ask... ;)
    >
    > I have a program in c which is working fine, it reads everything from
    > pipe, modifies output data a bit and writes to stdout.
    >
    > When it is executed without parameters, program works fine. But what if
    > I'd like to pass some extra configuration parameters to my program?
    >
    > 1. c:\> pipetest < file_in > file_out // this works
    > 2. c:\> pipetest /parameter < file_in // also works, writes
    > file_in and parameters to stdout
    > 3. c:\> pipetest /parameter < file_in > file_out // nothing happens !?
    >
    > Thanks anyone for kind advice and a nice day to all,
    > Y.
    >
    > // pitest.c
    > #include <stdio.h>
    > #include <fcntl.h>
    > #include <io.h>
    >
    > int main(int argc,char *argv[]) {
    > int ch,i;
    > for (i = 1; i < argc; i++) {
    > printf("%s\n",argv);
    > }
    > setmode(fileno(stdin), O_BINARY);
    > setmode(fileno(stdout), O_BINARY);
    > ch=getc(stdin);
    > while (!feof(stdin)) {
    > putc(ch,stdout);
    > }
    > }
    > // -------
    >


    Shouldn't the getc() call be inside the while loop?
    Barry, Oct 4, 2005
    #2
    1. Advertising

  3. Hake Guest

    "" <> wrote in
    news::

    > Hello, I'm new to c - so sorry if this is off topic here, please advice
    > at least where should i ask... ;)
    >
    > I have a program in c which is working fine, it reads everything from
    > pipe, modifies output data a bit and writes to stdout.
    >
    > When it is executed without parameters, program works fine. But what if
    > I'd like to pass some extra configuration parameters to my program?
    >
    > 1. c:\> pipetest < file_in > file_out // this works
    > 2. c:\> pipetest /parameter < file_in // also works, writes
    > file_in and parameters to stdout
    > 3. c:\> pipetest /parameter < file_in > file_out // nothing happens !?
    >
    > Thanks anyone for kind advice and a nice day to all,
    > Y.
    >
    > // pitest.c
    > #include <stdio.h>
    > #include <fcntl.h>
    > #include <io.h>
    >
    > int main(int argc,char *argv[]) {
    > int ch,i;
    > for (i = 1; i < argc; i++) {
    > printf("%s\n",argv);
    > }
    > setmode(fileno(stdin), O_BINARY);
    > setmode(fileno(stdout), O_BINARY);
    > ch=getc(stdin);
    > while (!feof(stdin)) {
    > putc(ch,stdout);
    > }
    > }


    a) Case #1 has no arguments to print because everything from the '<' over
    is not passed to your program.

    b) Case #3 will write "/parameter" to file_out, because that's where
    you've redirected stdout, STUPID.

    c) Your program will NOT copy stdin to stdout correctly; it will just
    copy the first byte from stdin over and over until you stop the program,
    you DUMBASS.

    d) You are a fucking moron. Forget C and go play with dolls or
    something.
    Hake, Oct 4, 2005
    #3
  4. Hake <> wrote:

    > b) Case #3 will write "/parameter" to file_out, because that's where
    > you've redirected stdout, STUPID.


    > (unwarranted flame snipped)


    I would suggest demonstrating some civility, lest you be dealt with in
    like fashion the next time a bug appears in your code. Not everyone
    has the luxury of skipping the "beginning programmer" stage as you
    apparently have.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 4, 2005
    #4
  5. <> wrote:

    > Hello, I'm new to c - so sorry if this is off topic here, please advice
    > at least where should i ask... ;)


    It isn't very topical; comp.unix.programmer is a good place to go
    (assuming, perhaps incorrectly, that you are using a flavor of Unix).
    You managed not to be totally off-topic, however...

    > // pitest.c
    > #include <stdio.h>
    > #include <fcntl.h>
    > #include <io.h>


    > int main(int argc,char *argv[]) {
    > int ch,i;
    > for (i = 1; i < argc; i++) {
    > printf("%s\n",argv);
    > }
    > setmode(fileno(stdin), O_BINARY);
    > setmode(fileno(stdout), O_BINARY);
    > ch=getc(stdin);
    > while (!feof(stdin)) {
    > putc(ch,stdout);
    > }
    > }


    Don't do it like that.

    while( (ch=getc(stdin)) != EOF ) {
    putc( ch, stdout );
    }


    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Oct 4, 2005
    #5
  6. Guest

    Hake wrote:
    > "" <> wrote in
    > news::
    >
    >
    >>Hello, I'm new to c - so sorry if this is off topic here, please advice
    >>at least where should i ask... ;)
    >>
    >>I have a program in c which is working fine, it reads everything from
    >>pipe, modifies output data a bit and writes to stdout.
    >>
    >>When it is executed without parameters, program works fine. But what if
    >>I'd like to pass some extra configuration parameters to my program?
    >>
    >>1. c:\> pipetest < file_in > file_out // this works
    >>2. c:\> pipetest /parameter < file_in // also works, writes
    >>file_in and parameters to stdout
    >>3. c:\> pipetest /parameter < file_in > file_out // nothing happens !?
    >>
    >>Thanks anyone for kind advice and a nice day to all,
    >>Y.
    >>
    >>// pitest.c
    >>#include <stdio.h>
    >>#include <fcntl.h>
    >>#include <io.h>
    >>
    >>int main(int argc,char *argv[]) {
    >> int ch,i;
    >> for (i = 1; i < argc; i++) {
    >> printf("%s\n",argv);
    >> }
    >> setmode(fileno(stdin), O_BINARY);
    >> setmode(fileno(stdout), O_BINARY);
    >> ch=getc(stdin);
    >> while (!feof(stdin)) {
    >> putc(ch,stdout);
    >> }
    >>}

    >
    >
    > a) Case #1 has no arguments to print because everything from the '<'

    over
    > is not passed to your program.


    this point was clear to me :)

    >
    > b) Case #3 will write "/parameter" to file_out, because that's where
    > you've redirected stdout, STUPID.


    Oh no, right! I must admit you are right :( :( I'm really a dumb :( What
    a contretemps....

    >
    > c) Your program will NOT copy stdin to stdout correctly; it will just
    > copy the first byte from stdin over and over until you stop the program,
    > you DUMBASS.


    The program I pasted here was wrong, original of my program is longer
    and it was not point to bore anyone with that, when that worked. My loop
    is working... my only mistake was I forgot to leave fgetc inside loop -
    in original it's there of course... :)

    >
    > d) You are a fucking moron. Forget C and go play with dolls or
    > something.


    And you are a monkey, not a human :) btw. I prefer trainspotting before
    playing with dolls... ;) But in some rare cases I need to make some
    handy program. I'm not a programmer (as everyone yet noticed:)

    Anyway, thank you for help, you are a bit rude, but you helped me, and
    that was what I really needed :D btw. the question *wasn't* "what you
    thing about my IQ", so you could have left the off topic notes for
    yourself... ;) But I must admit that I have showed up myself pretty bad :(

    J.
    , Oct 5, 2005
    #6
  7. Guest

    Christopher Benson-Manica wrote:
    > <> wrote:
    >
    >
    >>Hello, I'm new to c - so sorry if this is off topic here, please advice
    >>at least where should i ask... ;)

    >
    >
    > It isn't very topical; comp.unix.programmer is a good place to go
    > (assuming, perhaps incorrectly, that you are using a flavor of Unix).
    > You managed not to be totally off-topic, however...
    >


    Thank you ;)

    > Don't do it like that.
    >
    > while( (ch=getc(stdin)) != EOF ) {
    > putc( ch, stdout );
    > }
    >
    >


    Thank you for kind reply, I screwed "copy and paste" operation :(
    The piece of program I pasted here was wrong, original of my program is
    longer and it was not point to bore anyone with that, when that worked.
    My loop is working... my only mistake was I forgot to leave fgetc inside
    loop - in original it's there of course... :)

    J.
    , Oct 5, 2005
    #7
  8. Guest

    Barry wrote:
    > <> wrote in message news:...
    >
    >>Hello, I'm new to c - so sorry if this is off topic here, please advice
    >>at least where should i ask... ;)
    >>
    >>I have a program in c which is working fine, it reads everything from
    >>pipe, modifies output data a bit and writes to stdout.
    >>
    >>When it is executed without parameters, program works fine. But what if
    >>I'd like to pass some extra configuration parameters to my program?
    >>
    >>1. c:\> pipetest < file_in > file_out // this works
    >>2. c:\> pipetest /parameter < file_in // also works, writes
    >>file_in and parameters to stdout
    >>3. c:\> pipetest /parameter < file_in > file_out // nothing happens !?
    >>
    >>Thanks anyone for kind advice and a nice day to all,
    >>Y.
    >>
    >>// pitest.c
    >>#include <stdio.h>
    >>#include <fcntl.h>
    >>#include <io.h>
    >>
    >>int main(int argc,char *argv[]) {
    >> int ch,i;
    >> for (i = 1; i < argc; i++) {
    >> printf("%s\n",argv);
    >> }
    >> setmode(fileno(stdin), O_BINARY);
    >> setmode(fileno(stdout), O_BINARY);
    >> ch=getc(stdin);
    >> while (!feof(stdin)) {
    >> putc(ch,stdout);
    >> }
    >>}
    >>// -------
    >>

    >
    >
    > Shouldn't the getc() call be inside the while loop?
    >
    >


    Of course it should, sorry :( The piece of program I pasted here was
    wrong, original of my program is longer and it was not point to bore
    anyone with that, when that worked. My loop is working... my only
    mistake was I forgot to leave fgetc inside loop - in original it's there
    of course... :)

    I was confused with the pipes and one guy already found the reply :)

    J.
    , Oct 5, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lee, wonsun
    Replies:
    1
    Views:
    480
    Jack Klein
    Nov 2, 2004
  2. Steven K. Wong
    Replies:
    6
    Views:
    1,011
    Nobody
    Jan 10, 2010
  3. Phi!
    Replies:
    1
    Views:
    172
  4. cap
    Replies:
    1
    Views:
    100
    nobuyoshi nakada
    Dec 8, 2005
  5. Replies:
    1
    Views:
    217
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page