简体   繁体   English

缺乏“int main(int argc,char * argv [])”的知识

[英]On the lack of “int main(int argc, char* argv[])” knowledge

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 ie 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 "XY" points (like on a spreadsheet) and I what to plug that on "b.exe". 示例:假设“a.exe”给出了一组“XY”点(就像在电子表格上一样),我在“b.exe”上插入了什么。 Or say I use a bash/awk script to format the output format of "a.exe" to plot it on, say, gnuplot. 或者说我使用bash / awk脚本格式化“a.exe”的输出格式以将其绘制在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") 并制作了一个包含内容的文件(假设它是“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. 您在main函数中指定的参数处理参数,而不是管道。

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. 这些将由程序中的bolerplate代码解析,并在argv main函数时最终在argv数组中。

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.) (在指定程序时通常不需要.exe扩展名,我只是将其包含在此处以清楚地区分程序,参数和文件。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM