我有这个C程序从用户获取输入并将其发送到一个方法:如何在C中使用UNIX终端输入重定向?
#include <stdio.h>
#include <string.h>
#include "constants.h"
#include "lines.h"
#include "compare.h"
//gets arguments and sends to compare.c
int main() {
int op = 1;
char filename[20];
scanf("%d ", &op);
gets(filename);
if ((char) op + '0' < 49 || (char) op + '0' > 57) {
printf("Error: Invalid input");
exit(0);
}
readstdin(filename, op);
return 0;
}
而不是执行该程序并从stdin读取但是,我希望它从UNIX终端读取,使得:
./sort [field] < input_file
将读入文件。 (如果没有输入,[字段]是选项,默认值为1)。
例如执行在UNIX的C程序应该是这样的命令:
./sort 1 < test.txt
我如何去这样做呢?
任何帮助都非常熟悉 谢谢!
你想单独传递文件名还是从命令行传递完整数据? –
文件名和字段从命令行,但我想文件名和字段作为单独的值 –
然后只需使用命令行参数。在main()中使用argc&argv –