Kevag6
Kevag6

Reputation: 135

Piping to same c program using stdin and stdout

Let's say I have an executable called myProgram that reads from an input file and writes to an output file. A command line for this program looks like this:

./myProgram -o outputfile inputfile

The argument to the -o option specifies an output file name; if no such file exists then the program will create it.

What I was wondering is whether I can execute myProgram multiple times, piping the output of the one instance to the input of the next. For example,

./myProgram inputfile | ./myProgram -o outputfile

Is it possible to achieve this and if so, what would I have to implement? exec calls and forking? just simple read and write calls?

Upvotes: 3

Views: 2197

Answers (4)

éclairevoyant
éclairevoyant

Reputation: 410

You do not need to change your program.

In Unix, everything is (represented as) a file, including stdout and stdin.

If your program outputs to stdout, then running

./myProgram inputfile | ./myProgram -o outputfile /dev/stdin

will work. This uses stdin as the input "file" to the second program.

If your program requires some output file and does not normally output to stdout (not sure where else it'd output to, but whatever), then you can use

./myProgram -o /dev/stdout inputfile | ./myProgram -o outputfile /dev/stdin

to force it to output to the stdout "file", and then pipe that to the next command.

If you do want to change your program...

Make sure that it reads from stdin and outputs to stdout when no arguments are provided. Since you're programming in C, fgets() and fwrite() should suffice in most cases (and notice both of these commands are again reading from/writing to the file descriptors for stdin and stdout).

Upvotes: 0

Ctx
Ctx

Reputation: 18410

The short answer is: yes, you can do this.

However, your program should then read its input from stdin and write to stdout. This means, arguments as input or output file will no longer be necessary.

./myProgram < inputfile > outputfile

or, to illustrate the chaining,

./myProgram < inputfile | ./myProgram > outputfile

A widely spread combination is to read from stdin if no input file is specified and write to stdout if no outputfile or outputfile "-" is passed at the command line. This provides maximum flexibility.

Having said that, if it is sensible to chain multiple instances of the same program depends of course largely on what this program does. For example, for a sorting program this doesn't seem to make much sense of course ;)

Upvotes: 2

dbush
dbush

Reputation: 223699

Pipes work by chaining stdin and stdout of multiple programs together.

You need to modify your program to have the ability to read from stdin and write to stdout instead of to specific files. Then you can use pipes.

Upvotes: 3

Pierre Fran&#231;ois
Pierre Fran&#231;ois

Reputation: 6061

There is absolutely no problem to run two instances of your program myProgram together as you do with command

./myProgram inputfile | ./myProgram -o outputfile

Upvotes: 1

Related Questions