I have this C program that gets the input from the user and sends it to a method:
我有这个C程序从用户获取输入并将其发送到方法:
#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;
}
but instead of executing the program and reading from stdin, I want it to read from the unix terminal so that:
但是不是执行程序并从stdin读取,我希望它从unix终端读取,以便:
./sort [field] < input_file
will read into the file. ([field] is option if no input is put in, default is 1).
将读入该文件。 (如果没有输入,则[field]是选项,默认为1)。
For example the command to execute the C program in UNIX would look like this:
例如,在UNIX中执行C程序的命令如下所示:
./sort 1 < test.txt
How do I go about doing this?
我该怎么做呢?
Any help is much appreicated Thanks!
任何帮助都很有意义谢谢!
1 个解决方案
#1
4
For a start, you're getting your arguments the wrong way in your code. If what you're wanting is to run your program such as ./sort <option> <filename>
, then you don't use stdin
to retrieve these arguments.
首先,您在代码中以错误的方式获取参数。如果你想要的是运行你的程序,如./sort
The arguments in a C program are passed to main using the following function signature:
使用以下函数签名将C程序中的参数传递给main:
int main(int argc, char *argv[])
argc
is the number of command line arguments passed to the program, and argv
is the array of strings of those arguments.
argc是传递给程序的命令行参数的数量,argv是这些参数的字符串数组。
With a run of ./sort 5 test.in
:
运行./sort 5 test.in:
-
argc
will equal 3 -
argv[0]
will be"./sort"
-
argv[1]
will be"5"
-
argv[2]
will be"test.in"
argc等于3
argv [0]将是“./sort”
argv [1]将是“5”
argv [2]将是“test.in”
You should check that the value of argc
is 3 to ensure that 2 arguments have been passed ("5"
, "test.in"
), as well as the filename ("./sort"
) for a total of 3.
您应该检查argc的值是否为3以确保已传递2个参数(“5”,“test.in”),以及文件名(“./sort”),总共3个。
If you want to have optional fields, it would be better to have them after the compulsory ones, or better yet is to use something like getopt where you could instead have something like: ./sort --file test.in
or ./sort --opt 5 --file test.in
. It's probably unnecessary for this case, but it's an option.
如果你想拥有可选字段,最好在强制字段之后使用它们,或者更好的是使用像getopt这样的东西,你可以改为:./ sort -file test.in或./sort --opt 5 --file test.in.对于这种情况,这可能是不必要的,但它是一种选择。
You can parse the integer option using atoi
or strtol
, however you like, to convert it from a string (char*
) to an integral type and fopen
, fgets
, fclose
to read from the input file.
您可以使用atoi或strtol解析整数选项,无论如何,将其从字符串(char *)转换为整数类型,并使用fopen,fgets,fclose从输入文件中读取。
#1
4
For a start, you're getting your arguments the wrong way in your code. If what you're wanting is to run your program such as ./sort <option> <filename>
, then you don't use stdin
to retrieve these arguments.
首先,您在代码中以错误的方式获取参数。如果你想要的是运行你的程序,如./sort
The arguments in a C program are passed to main using the following function signature:
使用以下函数签名将C程序中的参数传递给main:
int main(int argc, char *argv[])
argc
is the number of command line arguments passed to the program, and argv
is the array of strings of those arguments.
argc是传递给程序的命令行参数的数量,argv是这些参数的字符串数组。
With a run of ./sort 5 test.in
:
运行./sort 5 test.in:
-
argc
will equal 3 -
argv[0]
will be"./sort"
-
argv[1]
will be"5"
-
argv[2]
will be"test.in"
argc等于3
argv [0]将是“./sort”
argv [1]将是“5”
argv [2]将是“test.in”
You should check that the value of argc
is 3 to ensure that 2 arguments have been passed ("5"
, "test.in"
), as well as the filename ("./sort"
) for a total of 3.
您应该检查argc的值是否为3以确保已传递2个参数(“5”,“test.in”),以及文件名(“./sort”),总共3个。
If you want to have optional fields, it would be better to have them after the compulsory ones, or better yet is to use something like getopt where you could instead have something like: ./sort --file test.in
or ./sort --opt 5 --file test.in
. It's probably unnecessary for this case, but it's an option.
如果你想拥有可选字段,最好在强制字段之后使用它们,或者更好的是使用像getopt这样的东西,你可以改为:./ sort -file test.in或./sort --opt 5 --file test.in.对于这种情况,这可能是不必要的,但它是一种选择。
You can parse the integer option using atoi
or strtol
, however you like, to convert it from a string (char*
) to an integral type and fopen
, fgets
, fclose
to read from the input file.
您可以使用atoi或strtol解析整数选项,无论如何,将其从字符串(char *)转换为整数类型,并使用fopen,fgets,fclose从输入文件中读取。