creating c programs to accpet data from a pipe - '|'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
it runs fine but when i try to run it after a pipe i get this weird output:
Code:
schneidz@lq> columnate.x hello world
hello world
schneidz@lq> echo hello world | columnate.x
_=columnate.x MANPATH=/opt/vintela/vgp/man:/opt/vas/man:/usr/share/man:/usr/share/man/info/en_US/a_doc_lib/cmds:/usr/share/man/info:/usr/lpp/X11/Xamples/man:/usr/lpp/ssp/perl/man:/usr/lpp/ssp/perl5/lib/man:/usr/lpp/ssp/perl5/man:/usr/lpp/ssp/man:/usr/lpp/perfrep/man:/usr/share/man:/usr/dt/man:/usr/nsr/nsr_extract/man:/usr/OV/man
can somebody tell me how do i make my code accept input from a pipe ?
I'm not sure right now, but I think that reading from a pipe is just reading from stdin. So I guess you just have to get you arguments from stdin instead of argv.
You can probably just test if there are any arguments provided on the command line, if not then read them from stdin and output like you already do.
it runs fine but when i try to run it after a pipe i get this weird output:
Code:
schneidz@lq> columnate.x hello world
hello world
schneidz@lq> echo hello world | columnate.x
_=columnate.x MANPATH=/opt/vintela/vgp/man:/opt/vas/man:/usr/share/man:/usr/share/man/info/en_US/a_doc_lib/cmds:/usr/share/man/info:/usr/lpp/X11/Xamples/man:/usr/lpp/ssp/perl/man:/usr/lpp/ssp/perl5/lib/man:/usr/lpp/ssp/perl5/man:/usr/lpp/ssp/man:/usr/lpp/perfrep/man:/usr/share/man:/usr/dt/man:/usr/nsr/nsr_extract/man:/usr/OV/man
can somebody tell me how do i make my code accept input from a pipe ?
thanks much,
~schneidz
A pipe redirects output to stdin (fd 0). To get the desired effect (changing command-line-arguments) use xargs (i.e., echo "hello world" | xargs columnate.x).
Firstly, your program is using command line arguments without first checking if they exist. This is why you get the junk output. When you use argv[n] you must check that you're not using more values in argv than it has in it, else you'll get pointers to something random, probably causing junk output like you get,m or maybe a segmentation fault (and your program crashing horribly).
If you want to print the command line arguments, you might do it like this:
Code:
#include <stdio.h>
int main(int argc, char **argv)
{
int i;
for (i = 1; i < argc; i++) {
printf("%s ", argv[i]);
}
printf("\n");
return 0;
}
Note that this has nothing to to with reading from a pipe! When you start a program from the shell like this:
Code:
wibble | wobble
...this means that the output of wibble (from the standard output file descriptor, 1) is connected to the input of wobble (the standard input file descriptor, 2). This has nothing to do with command line options.
In wobble, you would use the I/O function to read from the standard input file descriptor. There are many functions for doing this. If you want to get one character at a time, you might use fgetc, or one of it's friends, getc or getchar. There are several IO routines which may be suitable depending on the task. Here's an example wobble.c which changes all lower case letters to upper case letters and sends the output to standard output (which could then be piped into another process if you like).
Code:
#include <stdio.h>
int main(int argc, char **argv)
{
int c;
while ((c = getchar()) != EOF) {
if (c >= 'a' && c <= 'z') {
c -= 32;
}
putchar(c);
}
return (0);
}
Yes, you can use scanf, but beware! Your program has a security flaw! If someone passes an argument which is bigger than the sizes you have statically allocated to arg1 and arg2 (minus one character because a \0 is automatically appended by scanf), a buffer over-run will occur!
...this means that the output of wibble (from the standard output file descriptor, 1) is connected to the input of wobble (the standard input file descriptor, 2). This has nothing to do with command line options.
It connects to file descriptor 0 (stdin).
Quote:
Originally Posted by schneidz
i also tried xorgs and that is more an ideal solution (less re-coding/ compiling)
i love options... give me money
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.