473,378 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to read data from a bash pipe

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
Jul 23 '05 #1
13 4028

"j. del" <do*******@gmail.com> wrote in message
news:23**************************@posting.google.c om...
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
Jul 23 '05 #2
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
Jul 23 '05 #3
"j. del" <do*******@gmail.com> wrote in message
news:23**************************@posting.google.c om...

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
Jul 23 '05 #4
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
Jul 23 '05 #5
Ioannis Vranos <iv*@remove.this.grad.com> 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
Jul 23 '05 #6
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 <iv*@remove.this.grad.com> 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
Jul 23 '05 #7
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
Jul 23 '05 #8
"Ioannis Vranos" <iv*@remove.this.grad.com> 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
Jul 23 '05 #9
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
Jul 23 '05 #10

"Malte Starostik" <ma***@starostik.de> wrote in message
news:42********@olaf.komtel.net...
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
Jul 23 '05 #11
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
Jul 23 '05 #12
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
Jul 23 '05 #13

"Ioannis Vranos" <iv*@remove.this.grad.com> 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
Jul 23 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jenny | last post by:
Hi, I have a java socket program running on AIX 4.3.3.0 platform. It opens a socket and sends data to our customer over a leased fractional T1 line. The line is always connected. However,...
5
by: Jim | last post by:
Hi, I am trying to figure out a way to implement a timeout along with a read() call on an open file. It only has to work on linux, for now I am trying: ret = select.select( ,,, timeout ) if...
1
by: Peter Ammon | last post by:
I would like to read from a pipe in which data may arrive slowly. From experimenting, it looks like os.read() will block until it returns the maximum amount of data you asked for, in the second...
3
by: lpe540 | last post by:
Hi, I'm having trouble using istream to read in a file in its entirety on UNIX. I've written a dummy program that essencially reads in a file from stdin and writes it out to a file. When I cat a...
18
by: jas | last post by:
Hi, I would like to start a new process and be able to read/write from/to it. I have tried things like... import subprocess as sp p = sp.Popen("cmd.exe", stdout=sp.PIPE)...
8
by: collinm | last post by:
hi we use linux and c language under bash i do echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2 that send command to a led display (alpha sign communication)
2
by: ShawnD | last post by:
I'm having some issues when trying to read input off of a pipe using a python script. I'm trying to process packet data from tcpdump in real-time, so it's a filter that needs to read data while the...
4
by: 4zumanga | last post by:
I have a bunch of really horrible hacked-up bash scripts which I would really like to convert to python, so I can extend and neaten them. However, I'm having some trouble mapping some constructs...
6
by: placid | last post by:
Hi all, I have been looking into non-blocking read (readline) operations on PIPES on windows XP and there seems to be no way of doing this. Ive read that you could use a Thread to read from the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.