How to read data from a bash pipe

Discussion in 'C++' started by j. del, Mar 22, 2005.

  1. j. del

    j. del Guest

    I am just beginning to write programs... and my first task that I have
    set myself is to write a little program that will generate cryptic
    bywords from a source of text. A cryptic byword is basically a ceaser
    cypher but with randomly assigned letters rather than just shifted
    down by a value. So.. I am thinking that the BSD fortune game for the
    gnixes is a good source for the text to cypher. HOWEVER.. I am not
    sure how to read data in from another source into a C++ program..

    I would like to do ...
    $fortune | crypticbyword

    and have it output the cryptic byword. How does one read data from a
    bash pipe?

    Help?

    joshua
    j. del, Mar 22, 2005
    #1
    1. Advertising

  2. j. del

    Howard Guest

    "j. del" <> wrote in message
    news:...
    >I am just beginning to write programs... and my first task that I have
    > set myself is to write a little program that will generate cryptic
    > bywords from a source of text. A cryptic byword is basically a ceaser
    > cypher but with randomly assigned letters rather than just shifted
    > down by a value. So.. I am thinking that the BSD fortune game for the
    > gnixes is a good source for the text to cypher. HOWEVER.. I am not
    > sure how to read data in from another source into a C++ program..
    >
    > I would like to do ...
    > $fortune | crypticbyword
    >
    > and have it output the cryptic byword. How does one read data from a
    > bash pipe?
    >
    > Help?
    >
    > joshua


    I've never heard of a "bash pipe", (or many of those other terms, either).
    Isn't that something we used to smoke back in the '70s? :)

    But I believe that when you "pipe" data into an application, it simply
    appears in the standard input stream. Look up how to use std::cin. I think
    that's the answer.

    -Howard
    Howard, Mar 22, 2005
    #2
    1. Advertising

  3. j. del wrote:
    > I am just beginning to write programs... and my first task that I have
    > set myself is to write a little program that will generate cryptic
    > bywords from a source of text. A cryptic byword is basically a ceaser
    > cypher but with randomly assigned letters rather than just shifted
    > down by a value. So.. I am thinking that the BSD fortune game for the
    > gnixes is a good source for the text to cypher. HOWEVER.. I am not
    > sure how to read data in from another source into a C++ program..
    >
    > I would like to do ...
    > $fortune | crypticbyword
    >
    > and have it output the cryptic byword. How does one read data from a
    > bash pipe?
    >
    > Help?


    This has nothing to do with C++ language. Piping of command outputs is
    done at the OS level and the 'crypticbyword' simply gets the 'fortune's
    output as its standard input. Please get a book on programming your OS
    and read about pipes, or post to a newsgroup that deals with your OS.

    V
    Victor Bazarov, Mar 22, 2005
    #3
  4. "j. del" <> wrote in message
    news:...
    >
    > and have it output the cryptic byword. How does one read data from a
    > bash pipe?


    AFAIK, the data put into the pipe is just written to the standard input stream
    (cin) for the next program.

    - JFA1
    James Aguilar, Mar 22, 2005
    #4
  5. j. del wrote:

    > I am just beginning to write programs... and my first task that I have
    > set myself is to write a little program that will generate cryptic
    > bywords from a source of text. A cryptic byword is basically a ceaser
    > cypher but with randomly assigned letters rather than just shifted
    > down by a value. So.. I am thinking that the BSD fortune game for the
    > gnixes is a good source for the text to cypher. HOWEVER.. I am not
    > sure how to read data in from another source into a C++ program..
    >
    > I would like to do ...
    > $fortune | crypticbyword
    >
    > and have it output the cryptic byword. How does one read data from a
    > bash pipe?
    >
    > Help?



    int main(int argc, char *argv[]) in the second program I suppose.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 22, 2005
    #5
  6. j. del

    j. del Guest

    Re: How to read data from a bash pipe -- thank you

    Ioannis Vranos <> wrote in message news:<1111533275.67142@athnrd02>...
    > j. del wrote:
    >
    > > I am just beginning to write programs... and my first task that I have
    > > set myself is to write a little program that will generate cryptic
    > > bywords from a source of text. A cryptic byword is basically a ceaser
    > > cypher but with randomly assigned letters rather than just shifted
    > > down by a value. So.. I am thinking that the BSD fortune game for the
    > > gnixes is a good source for the text to cypher. HOWEVER.. I am not
    > > sure how to read data in from another source into a C++ program..
    > >
    > > I would like to do ...
    > > $fortune | crypticbyword
    > >
    > > and have it output the cryptic byword. How does one read data from a
    > > bash pipe?
    > >
    > > Help?

    >
    >
    > int main(int argc, char *argv[]) in the second program I suppose.



    This looks like what I'm looking for. The question could be phrased
    'how to read data from stdin as a sort of argument' maybe. I don't
    think it has fsckall to do with the OS, since Win32 shells have pipes
    as well as C, or any other command line. If I had the money for a
    book, I would buy one. I am sure that I will. Thank you Ioannis.

    J
    j. del, Mar 23, 2005
    #6
  7. j. del

    j. del Guest

    Re: How to read data from a bash pipe -- thank you

    read

    "I don't think it has fsckall to do with the OS, since Win32 shells
    have pipes as well as C, or any other command line."

    as "since Win32 shells have pipes as well as C shell"

    for clarification.

    Ioannis Vranos <> wrote in message news:<1111533275.67142@athnrd02>...
    > j. del wrote:
    >
    > > I am just beginning to write programs... and my first task that I have
    > > set myself is to write a little program that will generate cryptic
    > > bywords from a source of text. A cryptic byword is basically a ceaser
    > > cypher but with randomly assigned letters rather than just shifted
    > > down by a value. So.. I am thinking that the BSD fortune game for the
    > > gnixes is a good source for the text to cypher. HOWEVER.. I am not
    > > sure how to read data in from another source into a C++ program..
    > >
    > > I would like to do ...
    > > $fortune | crypticbyword
    > >
    > > and have it output the cryptic byword. How does one read data from a
    > > bash pipe?
    > >
    > > Help?

    >
    >
    > int main(int argc, char *argv[]) in the second program I suppose.





    This looks like what I'm looking for. The question could be phrased
    'how to read data from stdin as a sort of argument' maybe. I don't
    think it has fsckall to do with the OS, since Win32 shells have pipes
    as well as C, or any other command line. If I had the money for a
    book, I would buy one. I am sure that I will. Thank you Ioannis.

    J
    j. del, Mar 23, 2005
    #7
  8. Re: How to read data from a bash pipe -- thank you

    j. del wrote:

    > read
    >
    > "I don't think it has fsckall to do with the OS, since Win32 shells
    > have pipes as well as C, or any other command line."
    >
    > as "since Win32 shells have pipes as well as C shell"
    >
    > for clarification.
    >
    >>int main(int argc, char *argv[]) in the second program I suppose.

    >
    >
    >
    >
    >
    > This looks like what I'm looking for. The question could be phrased
    > 'how to read data from stdin as a sort of argument' maybe. I don't
    > think it has fsckall to do with the OS, since Win32 shells have pipes
    > as well as C, or any other command line. If I had the money for a
    > book, I would buy one. I am sure that I will. Thank you Ioannis.



    Actually this has not anything to do with cin (standard input stream,
    usually the keyboard).


    This is how to get the command line arguments in a program. If argc!=0,
    argv[0] is the name of the program itself as used in the command line,
    argv[1] the first argument etc, and argc the total amount of them.
    argv[argc]=='\0' ('\0' has the value 0 by the way).


    So if you create a program test and you do:

    C:\c>test -a -v


    you get:


    argv[0]== "test"
    argv[1]== "-a"
    argv[2]== "-v"
    argv[3]== "" (or '\0' or 0).



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 24, 2005
    #8
  9. Re: How to read data from a bash pipe -- thank you

    "Ioannis Vranos" <> wrote...
    > [..]
    > So if you create a program test and you do:
    >
    > C:\c>test -a -v
    >
    >
    > you get:
    >
    >
    > argv[0]== "test"


    There is no guarantee on that. It can be "" just as well.

    > argv[1]== "-a"
    > argv[2]== "-v"
    > argv[3]== "" (or '\0' or 0).


    Actually there is no "or" about it. argv[argc] is 0. Not "", not
    '\0', null. See the Standard, the subclause about the 'main' function.
    And by the way, C strings do not compare very well with the equality
    operator. Use something different next time please. For example,
    <<argv[1] points to "-a">>. Otherwise newbies will try

    if (argv[0]== "-a")

    and complain about it when it doesn't work with reference to your
    liberal use of operator==.

    V
    Victor Bazarov, Mar 24, 2005
    #9
  10. Re: How to read data from a bash pipe -- thank you

    Ioannis Vranos schrieb:
    > j. del wrote:
    >
    >> read
    >> "I don't think it has fsckall to do with the OS, since Win32 shells
    >> have pipes as well as C, or any other command line."
    >>
    >> as "since Win32 shells have pipes as well as C shell"
    >>
    >> for clarification.

    >
    >>> int main(int argc, char *argv[]) in the second program I suppose.

    >>
    >> This looks like what I'm looking for. The question could be phrased
    >> 'how to read data from stdin as a sort of argument' maybe. I don't
    >> think it has fsckall to do with the OS, since Win32 shells have pipes
    >> as well as C, or any other command line. If I had the money for a
    >> book, I would buy one. I am sure that I will. Thank you Ioannis.

    >
    > Actually this has not anything to do with cin (standard input stream,
    > usually the keyboard).


    Yes it has. The question was about pipes. While C++ doesn't know about
    pipes, it knows about standard input, accessible via std::cin. And
    standard input happens to be exactly where the data piped into a program
    by the shell ends up. "Usually the keyboard" is a little imprecise. At
    the very least there's a line buffer between the keyboard and stdin.
    More likely it's some kind of terminal or console device or mechanism.
    A console window in Windows is one such mechanism. Either way, the
    keyboard is not the only possible origin of what arrives at stdin; as
    far as C++ is concerned, stdin is simply a source of input data.

    > This is how to get the command line arguments in a program. If argc!=0,
    > argv[0] is the name of the program itself as used in the command line,
    > argv[1] the first argument etc, and argc the total amount of them.
    > argv[argc]=='\0' ('\0' has the value 0 by the way).
    >
    >
    > So if you create a program test and you do:
    >
    > C:\c>test -a -v
    >
    >
    > you get:
    >
    >
    > argv[0]== "test"
    > argv[1]== "-a"
    > argv[2]== "-v"
    > argv[3]== "" (or '\0' or 0).


    Correct for program arguments. When the OP wrote "sort of argument",
    that didn't necessarily refer to argv.

    Cheers,
    Malte
    Malte Starostik, Mar 24, 2005
    #10
  11. j. del

    Howard Guest

    Re: How to read data from a bash pipe -- thank you

    "Malte Starostik" <> wrote in message
    news:42421ec1$...
    >>
    >>>> int main(int argc, char *argv[]) in the second program I suppose.
    >>>
    >>> This looks like what I'm looking for. The question could be phrased
    >>> 'how to read data from stdin as a sort of argument' maybe. I don't
    >>> think it has fsckall to do with the OS, since Win32 shells have pipes
    >>> as well as C, or any other command line. If I had the money for a
    >>> book, I would buy one. I am sure that I will. Thank you Ioannis.

    >>
    >> Actually this has not anything to do with cin (standard input stream,
    >> usually the keyboard).

    >
    > Yes it has. The question was about pipes.


    The question was about pipes, but you misssed the point. The proposed
    *answer* by Ioannis was using command-line arguments, and Victor pointed out
    that that had nothing to to with cin (which is how piped-in data can be
    read, via standard input).

    -Howard
    Howard, Mar 24, 2005
    #11
  12. Re: How to read data from a bash pipe -- thank you

    Howard wrote:
    > [...] The proposed
    > *answer* by Ioannis was using command-line arguments, and Victor pointed out


    No... Ioannis actually pointed it to himself, I guess. I just caught
    a couple of other discrepancies in his post.

    > that that had nothing to to with cin (which is how piped-in data can be
    > read, via standard input).


    V
    Victor Bazarov, Mar 24, 2005
    #12
  13. Re: How to read data from a bash pipe -- thank you

    Victor Bazarov wrote:

    > No... Ioannis actually pointed it to himself, I guess. I just caught
    > a couple of other discrepancies in his post.



    And I got a head-ache. :)

    Actually I made a mistake, piping passes a program's standard output to
    the other's standard input (cin in C++). I do not how I got confused and
    thought that the output is passed to char *argv[] somehow.



    --
    Ioannis Vranos

    http://www23.brinkster.com/noicys
    Ioannis Vranos, Mar 25, 2005
    #13
  14. j. del

    Howard Guest

    Re: How to read data from a bash pipe -- thank you

    "Ioannis Vranos" <> wrote in message
    news:1111721871.123532@athnrd02...
    > Victor Bazarov wrote:
    >
    >> No... Ioannis actually pointed it to himself, I guess. I just caught
    >> a couple of other discrepancies in his post.

    >
    >
    > And I got a head-ache. :)
    >
    > Actually I made a mistake, piping passes a program's standard output to
    > the other's standard input (cin in C++). I do not how I got confused and
    > thought that the output is passed to char *argv[] somehow.
    >


    Maybe from smoking that bash pipe...? ;-)

    -Howard
    Howard, Mar 28, 2005
    #14
    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. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,266
    William Park
    Jul 14, 2005
  2. hofer
    Replies:
    11
    Views:
    2,598
  3. Fred

    executing bash comands with pipe

    Fred, Jun 26, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    136
    J├╝rgen Exner
    Jun 26, 2007
  4. Tarek Elganainy

    pipe perl output to a bash command error

    Tarek Elganainy, Feb 2, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    308
  5. Peng Yu
    Replies:
    1
    Views:
    237
    Alan Curry
    Jun 6, 2010
Loading...

Share This Page