我想知道是否有人可以解释'<'和'&>'这里的陈述意味着什么。
./pa1 < test.lig &> test.out
这是C程序前端的代码。这是一个更新,显示我如何读取命令行参数以提供给程序以进行输入输出操作。
#include "tokens.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
extern FILE *yyin;
extern FILE *yyout;
int main(int argc, char** argv){
if(argc == 3){
printf("Input file = %s\n", argv[1]);
yyin = fopen(argv[1], "r");
yyout = fopen(argv[2], "w");
if (yyin == 0 || yyout == 0) {
printf("Error: %s\n", strerror(errno));
yyin = stdin;
exit(0);
}else{
fprintf(yyout, "File %s opened!\n", argv[1]);
}
}else{
exit(1);
}
//Variables
/*
Integers
1) tokenCount - Counts the number of tokens present in the file.
2) lexReturnToken - Checks to see if it is a valid token (not 999 according to tokens.h)
and then checks to see if it is a bracket, parentheses or curly so proper checks may be called.
*/
int tokenCount = 0, lexReturnToken;
//Standard Output Information
while(1){
lexReturnToken = yylex();
if(lexReturnToken > 0){
//Token Count
if(lexReturnToken != 999){
tokenCount++;
}//else{
// }
//}
}else{
printf("Total tokens: %d\n", tokenCount);
break;
}
}
//Bool Main Detection (preceded by function)
//Bool Bracket, Parentheses, Curly Match
return 0;
}
答案 0 :(得分:1)
<
正在接受来自文件的输入,在这种情况下,将test.lig
中的任何内容发送到pa1
。正如您所料,>
正在重定向输出,将您通常看到的任何内容从屏幕上的pa1
发送到文件test.out
。
&>
正在向test.out发送标准输出AND错误,没有任何错误仍然会显示在终端中并且不会污染输出文件。
答案 1 :(得分:0)
这意味着./pa1程序的输入位于test.lig文件中,输出将是test.out文件
答案 2 :(得分:0)
获取文件test.lig的内容并将其作为输入传递给./pa1,然后将输出推送到test.out。
&amp;&gt;告诉它将stdout和stderr连接到文件而不是stdout。
答案 3 :(得分:0)
重定向是设置stdin和stdout,但是你的程序会打开在命令行上命名的文件 - 所以它不是使用 stdin和stdout。
运行./pa1 <test.lig &>test.out
时,<test.lig
和&>test.out
重定向被解析为shell的命令 - 因此它们不包含在argv中,只包含{{1 }}
如果您希望重定向有效,那么当{"./pa1", NULL}
显示未提供任何参数时,请确保将argc
作为yyin
和stdin
继续作为{ {1}},而不是像现在这样调用yyout
。