I'm a Physicist and, usually, I just want to get the calculations done and that's all I really need. Nevertheless I had been using
int main(int argc, char* argv[])
and its very pleasing how the binary becomes flexible in levels that I didn't expect i.e. initializing variables
~$ ./program.exe a b c (a, b and c are numbers in this case, not letters ok?)
So the question is
*How do I "parse" or "pipe" things from one to another in this manner (or other) without using files?*
Example: say that "a.exe" gives a set of "X-Y" points (like on a spreadsheet) and I what to plug that on "b.exe". Or say I use a bash/awk script to format the output format of "a.exe" to plot it on, say, gnuplot.
-----------------------------------------
Hi again. I'm trying to do this but, I've still got problems.
To see what I'm doing wrong I wrote the simplest program
#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char* argv[]){
for (int i = 0; i <= argc; i++)
cout << "argv[" << i << "]= " << argv[i] << endl;
getchar();
return 0;
}
and made a file with the content (lets say it is "test.dat")
1
2
3
4
5
6
since I don't want to read the content of the file on my program, I just want to "throw it" to it, so I've tried
more test.dat | ./program.exe
or even
./program.exe << EOF
1 2 3 4 5
EOF
but it doesn't work and I thought it should.
Thanks again.
You are mixing the two concepts of arguments and piping.
The parameters that you specify in the main
function handles arguments, not piping.
The "pipe" character |
is used to do piping, and that doesn't affect the arguments, it affects the input and output stream. The <
and >
can be used to pipe from and to a file.
What you append after a program call is arguments, example:
progam.exe arg1 arg2 arg3
These will be parsed by the bolerplate code in the program, and end up in the argv
array when the main
function is called.
When you use the <
character to pipe a file into a program, the content of the file will be sent to the input stream of the program:
program.exe < data.txt
The program will not have console input (screen and keyboard), instead it's fed data from the file.
When you use the >
character to pipe to a file, the output of the program goes to the file instead of the console. Example:
program.exe > result.txt
When you use the |
character to pipe from one program to another, the output stream of the first program is the input stream of the other program. Example:
program.exe | sort.exe
Everything that the first program writes to the output stream goes into the input stream of the other program. If you don't specify any input or output files, the first program has console input, and the second program has console output. So, the data flows through the two programs like this:
[console] --> [program.exe] --> [sort.exe] --> [console]
You can pipe several programs, so that the data flows from the first program to the last. Example:
program.exe | sort.exe | more.exe
The data then flows throught all three programs:
[console] --> [program.exe] --> [sort.exe] --> [more.exe] --> [console]
(The .exe
extension is normally not needed when specifying a program, I just included it here to distinguish clearly between programs, arguments and files.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With