Piping to provide a file as input to a C program - c++

I have this set of .gz files and inside each of them is a single text file. This text file needs to be used in a C program. The following code solves this problem somehow where parameters 1 and 2 are integers which I'm receiving as arguments for the C program (argc, argv[]) in main().
gzip -dc xyz.txt.gz | ./program parameter1 parameter2
Can someone explain how the above code works in command line?
How does the text file automatically get passed to the program?
Do I need to write extra code in the C program to receive this text file?

The shell connects the stdout of one command directly to the stdin of the other command through a pipe(7). Neither program has to do anything out of the ordinary to take advantage of this.

Related

Program that writes to /dev/stdout: how to send EOF?

I have a program that writes data to a file. Normally, the file is on disk, but I am experimenting with writing to /dev/stdout. Currently, when I do this, the program will not exit until I press Ctrl-C . Is there a way for the program to signal that the output is done ?
Edit:
Currently, a disk file is opened via fopen(FILE_NAME), so I am now trying to pass in /dev/stdout as FILE_NAME.
Edit 2:
command line is
MY_PROGRAM -i foo -o /dev/stdout > dump.png
Edit 3:
It looks like the problem here is that stdout is already open, and I am opening it a second time.
The EOF condition on a FIFO (which, if you're piping from your program into something else, is what your stdout is) is set when no file handles are still open for write.
In C, the standard-library is fclose(stdout), whereas the syscall interface is close(1) -- if you're using fopen(), you'll want to pair it with fclose().
If you're also doing a separate outFile = fopen("/dev/stdout", "w") or similar, then you'll need to close that copy as well: fclose(outFile) as well as fclose(stdout).

Passing parameters to C++ compiled exe through cmd

For Example, I have compiled a C++ program through Cygwin g++ on my Windows PC. It accepts (cin) two strings and parses it to return a single string.
I am thinking of using it in a batch file. Is there any way I can return the result to the .bat program directly by passing the two strings as arguments without actually executing all the input dialogues. (Something like "asd.exe -param1,param2" )
Is it to be implemented in the cpp code end. If so, give me a start where to look upon.
To write to stdin u redirect the stdin using
so
prog<"params"
will execute prog and readin String "params" from std::cin
same as redirecting stdout to file.
The other option would be to put the input parameters as arguments to main
main(argv,argc){
and read argc as the array of string arguments to the program

How can I print out the file I just inputted into my program in C++/C [duplicate]

This question already has answers here:
In C how do I print filename of file that is redirected as input in shell
(9 answers)
Closed 7 years ago.
I'm trying to make my program output everything I typed into the command line but the file I streamed to it is not printing out because it doesn't get stored in argv. Here is how I execute:
Input:
./program < file.txt
Expected output:
./program < file.txt
Actual output:
./program
Just to be clear I don't want to print out what is in the file. I only want to print out the name of the file.
File redirection is handled by the shell, not by the program. When the shell sees "<" it basically says "when you start this program, map stdin to this file instead of the terminal". So, under the hood, the shell does fork(); followed by closing and opening the file in it's place, then calls execv() or similar to actually execute the program. Similarly, if you do ./program *.txt, by the time the program sees the command line, *.txt has been expanded to all of the matching filenames in the directory.
I doubt, it's a stream of data, filename is not being streamed to the program
Besides, I don't think you should look for the input in argv, you should read stream contents from stdin, e.g. using scanf's

Input/output redirect from a command-line executable to file

How can I save all the input(cin) and output(cout, cerr) from a program whose input is taken from file(using "<")? I would like the input and output to be in order(so each input is followed by corresponding output as if I were typing the input in myself).
I tried ">" to output everything to a file, but that only saves standard output(no input/cerr), and just plainly copying the command line output still only gives the output without the input(because of how "<" works).
Is there a way to write everything(output+input) to file in order?
EDIT: edited for clarity
EDIT2: I just realized that it's impossible to do what I'm trying to do since the console does not know anything about when the commands would actually be entered. I'll have to manually enter commands and use the "script" command to actually log all input/output.
You need to add cerr to the stream
command > file 2&>1
This means put 2 (stderr) to 1 (stdout) as well.

Control a shell program through command line, giving it multiple instructions/data

I need to control a program in c++ (windows), I need to call it, then pass data to it as I collect it, finally after a certain command that program will use that data.
I need to open the prog.exe and then line by line or value by value supply it information, it works manually through cmd.
I have tried system() but this will stop after I open the program.
I need something like this.
//call it
prog.exe
//add data
DataStart
Data 1 [2 34 454 5]//etc
DataEnd //the program will take it from here.
all being passed though command line
There are different ways you could do this - if your program needs to execute part of the way through your code before getting the data as input, you can just use standard input, and prompt the user to type the data. If you want to use variable values for the input, but you will know them before execution, you can pass the information as command line arguments, where you will execute like so
prog.exe 1 2 3
and your program will access the data via argv[i] where i corresponds to each command line argument.
have your program read from standard input, and from the command line 'pipe' the result of the other program to yours
eg.
datagenerator.exe | prog.exe
assuming that datagenerator.exe writes to standard output, the | character will redirect the output to prog.exe's standard input