I want to write a programme where I compile a C programme, and save it where BASH recognises programmes are (/usr/bin or somewhere).
For usage I want to run in the terminal
$ c_programme <file_input> | <programme_to_pipe_stdout>
I can see that with the popen() function I can use ffmpeg to process video:
FILE *pipein = popen("ffmpeg -i teapot.mp4 -f image2pipe -vcodec rawvideo -pix_fmt rgb24 -", "r");
FILE *pipeout = popen("ffmpeg -y -f rawvideo -vcodec rawvideo -pix_fmt rgb24 -s 1280x720 -r 25 -i - -f mp4 -q:v 50 -an -vcodec mpeg4 output.mp4", "w");
But in the above teapot.mp4 was already specified in the code, not input into the C programme when the programme was run in BASH. How can I make a variable in the C code which will read from the shell?
Similarly, I don't want to output to a file, I just want to output to stdout, so I can pipe it. How would I do this?
Thanks!
How can I make a variable in the C code which will read from the shell?
A C program can get the arguments passed to it on the command line by declaring main
as int main(int argc, char *argv[])
, after which argv[0]
refers to the name of the program, argv[1]
is the first argument, argv[2]
is the second, and so on up to argv[argc-1]
. The end is also indicate by argv[argc]
being a null pointer.
For example, the output of this program:
#include <stdio.h>
int main(int argc, char *argv[])
{
for (int i = 0; i < argc; ++i)
printf("argv[%d] = %s.\n", i, argv[i]);
}
when executed as foo Argument1 Argument2
is:
argv[0] = foo. argv[1] = Argument1. argv[2] = Argument2.
Similarly, I don't want to output to a file, I just want to output to stdout, so I can pipe it. How would I do this?
Use printf
, puts
, or putchar
.