The lack of "int main (int argc, char * argv [])" knowledge

I am a physicist, and as a rule, I just want to do the calculations and all that I really need. However, I used

int main(int argc, char* argv[]) 

and it’s very nice how the binary becomes flexible in levels that I did not expect, that is, variable initialization

 ~$ ./program.exe abc (a, b and c are numbers in this case, not letters ok?) 

So the question is:

* How to “parse” or “translate” things with each other in this way (or another) without using files? *

Example: let's say that “a.exe” gives a set of points “XY” (for example, on a spreadsheet), and I have to connect this to “b.exe”. Or to say that I use a bash / awk script to format the output format "a.exe" to build it, say, gnuplot.

-----------------------------------------

Hey. I am trying to do this, but I still have problems.

To find out what I am doing wrong, I wrote a simple 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 contents (let's say this is "test.dat")

  1 2 3 4 5 6 

since I don’t want to read the contents of the file in my program, I just want to “drop” it, so I tried

 more test.dat | ./program.exe 

or even

  ./program.exe << EOF 1 2 3 4 5 EOF 

but it does not work, and I thought it should be.

Thanks again.

+4
source share
1 answer

You mix two concepts of arguments and pipelines.

The parameters you specified in the main function handle the arguments, not piping.

Pipe symbol | used to create pipelines, and this does not affect the arguments, it affects the input and output stream. < and > can be used to transfer from file to file.

What you add after calling the program is an argument, for example:

 progam.exe arg1 arg2 arg3 

They will be analyzed using the connector code in the program and will end up in the argv array when the main function is called.

When you use the < symbol to transfer a file to the program, the contents of the file will be sent to the program input stream:

 program.exe < data.txt 

The program will not have access to the console (screen and keyboard), instead, it will load data from the file.

When you use the > symbol to connect to a file, the program output goes to the file, not to the console. Example:

 program.exe > result.txt 

When you use the symbol | for transfer from one program to another, the output stream of the first program is the input stream of another program. Example:

 program.exe | sort.exe 

Everything that the first program writes to the output stream enters the input stream of another program. If you do not specify any input or output files, the first program has console input, and the second has console input. Thus, data is transmitted through two programs, for example:

 [console] --> [program.exe] --> [sort.exe] --> [console] 

You can broadcast several programs so that the data passes from the first program to the last. Example:

 program.exe | sort.exe | more.exe 

Then the data is transmitted through all three programs:

 [console] --> [program.exe] --> [sort.exe] --> [more.exe] --> [console] 

(The .exe extension is usually not required when specifying a program, I just included it here to clearly distinguish between programs, arguments, and files.)

+10
source

Source: https://habr.com/ru/post/1379402/


All Articles