How to read data from a bash pipe

J

j. del

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
 
H

Howard

j. del said:
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
 
V

Victor Bazarov

j. del said:
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
 
J

James Aguilar

j. del said:
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
 
I

Ioannis Vranos

j. del said:
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.
 
J

j. del

Ioannis Vranos said:
j. del said:
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

j. del

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 said:
j. del said:
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
 
I

Ioannis Vranos

j. del said:
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).
 
V

Victor Bazarov

Ioannis Vranos said:
[..]
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
 
M

Malte Starostik

Ioannis said:
j. del said:
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
 
H

Howard

Malte Starostik said:
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
 
V

Victor Bazarov

Howard said:
[...] 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
 
I

Ioannis Vranos

Victor said:
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.
 
H

Howard

Ioannis Vranos said:
Victor said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top