I am writing a program in C on Linux, and I want to let the user specify a text file to be opened from the terminal. So the user types:
我在Linux上用C编写程序,我想让用户指定一个从终端打开的文本文件。所以用户输入:
./program < nums.txt
And the program will read whats in that .txt file. Currently, I specify the filename to be opened myself, using the code below:
程序将读取该.txt文件中的内容。目前,我使用以下代码指定要自行打开的文件名:
fp = fopen("nums.txt","r");
Note that nums.txt just contains two numbers separated by a space which are used for a min and max value.
请注意,nums.txt只包含两个用空格分隔的数字,用于最小值和最大值。
I am just learning C and am uncertain how to go about doing this. I've searched Stack Overflow and used search engines but still can't solve this issue.
我只是在学习C而且不确定如何去做。我搜索过Stack Overflow并使用了搜索引擎,但仍然无法解决这个问题。
Thanks.
2 个解决方案
#1
1
It may help to understand how shell input redirection works.
了解shell输入重定向的工作原理可能会有所帮助。
For the command line you gave, the shell is going to open the file and hook it up to the standard input of the process. C already has a stream for the standard input, so you can just consume it like this, no fopen
needed:
对于您提供的命令行,shell将打开该文件并将其连接到该进程的标准输入。 C已经有一个标准输入流,所以你可以像这样使用它,不需要fopen:
fp = stdin;
On the other hand, if you want the user to run your program like this:
另一方面,如果您希望用户像这样运行您的程序:
./program nums.txt
Then you're going to want to use the argc
and argv
parameters of your main
function, something like:
那么你将要使用主函数的argc和argv参数,例如:
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
FILE *fp = fopen(argv[1], "r");
...
The argc
variable contains the number of parameters passed and argv
is an array with each parameter as a string. The name used to invoke the program is usually the first element (argv[0]
) which is why I used argv[1]
. And, as always when working with arrays, make sure you don't try to access past the end.
argc变量包含传递的参数数量,argv是一个数组,每个参数都作为字符串。用于调用程序的名称通常是第一个元素(argv [0]),这就是我使用argv [1]的原因。而且,与使用数组时一样,请确保您不要尝试访问结束。
#2
0
Here is how to read the first character of standard input (use as ./program <nums.txt
):
以下是如何读取标准输入的第一个字符(用作./program
#include <stdio.h>
int main(int argc, char **argv) {
FILE *f = stdin;
int c = getc(f);
/* int c = getchar(); -- Same as above. */
if (c >= 0) printf("%c\n", c);
return 0;
}
Here is how to read the first character of the file specified in the command-line (use as ./program nums.txt
):
以下是如何读取命令行中指定的文件的第一个字符(用作./program nums.txt):
#include <stdio.h>
int main(int argc, char **argv) {
FILE *f = fopen(argv[1], "r");
/* TODO: Handle f == NULL. */
int c = getc(f);
if (c >= 0) printf("%c\n", c);
return 0;
}
#1
1
It may help to understand how shell input redirection works.
了解shell输入重定向的工作原理可能会有所帮助。
For the command line you gave, the shell is going to open the file and hook it up to the standard input of the process. C already has a stream for the standard input, so you can just consume it like this, no fopen
needed:
对于您提供的命令行,shell将打开该文件并将其连接到该进程的标准输入。 C已经有一个标准输入流,所以你可以像这样使用它,不需要fopen:
fp = stdin;
On the other hand, if you want the user to run your program like this:
另一方面,如果您希望用户像这样运行您的程序:
./program nums.txt
Then you're going to want to use the argc
and argv
parameters of your main
function, something like:
那么你将要使用主函数的argc和argv参数,例如:
int main(int argc, char **argv)
{
if (argc < 2)
return 1;
FILE *fp = fopen(argv[1], "r");
...
The argc
variable contains the number of parameters passed and argv
is an array with each parameter as a string. The name used to invoke the program is usually the first element (argv[0]
) which is why I used argv[1]
. And, as always when working with arrays, make sure you don't try to access past the end.
argc变量包含传递的参数数量,argv是一个数组,每个参数都作为字符串。用于调用程序的名称通常是第一个元素(argv [0]),这就是我使用argv [1]的原因。而且,与使用数组时一样,请确保您不要尝试访问结束。
#2
0
Here is how to read the first character of standard input (use as ./program <nums.txt
):
以下是如何读取标准输入的第一个字符(用作./program
#include <stdio.h>
int main(int argc, char **argv) {
FILE *f = stdin;
int c = getc(f);
/* int c = getchar(); -- Same as above. */
if (c >= 0) printf("%c\n", c);
return 0;
}
Here is how to read the first character of the file specified in the command-line (use as ./program nums.txt
):
以下是如何读取命令行中指定的文件的第一个字符(用作./program nums.txt):
#include <stdio.h>
int main(int argc, char **argv) {
FILE *f = fopen(argv[1], "r");
/* TODO: Handle f == NULL. */
int c = getc(f);
if (c >= 0) printf("%c\n", c);
return 0;
}