Linux 从作为命令行参数传递的文件中读取
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5123281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading from files passed as command line arguements
提问by Raven Dreamer
I am trying to parse a given textfile, but so far, my program does not seem to be reading properly.
我正在尝试解析给定的文本文件,但到目前为止,我的程序似乎没有正确读取。
#include <stdio.h>
int main(int argc, char *argv[])
{
FILE *fr; //file pointer
int buildingFloors = 1;
printf("sanity check\n");
fr = fopen (argv[0], "r");
fscanf(fr, "%d", &buildingFloors );
printf("%d\n", buildingFloors);
fclose(fr);
return 0;
}
I compile the program and run it on my redhat linux machine with the following command:
我编译程序并使用以下命令在我的 redhat linux 机器上运行它:
./sjf file.text
file.text is a text document with a "4" as the first character. So I would expect my output to be
file.text 是第一个字符为“4”的文本文档。所以我希望我的输出是
sanity check
4
However, when I run my program I instead get
但是,当我运行我的程序时,我反而得到
sanity check
1
Which implies that fscanf didn't properly read in the first character -- 4. Do I have some syntax error that's preventing the expected code functionality? Am I supposed to scanf for a character, and then convert that to an int somehow?
这意味着 fscanf 没有正确读取第一个字符—— 4. 我是否有一些语法错误阻止了预期的代码功能?我应该扫描一个字符,然后以某种方式将其转换为 int 吗?
采纳答案by Akusete
One thing which immediatly comes to mind is that the program args include the executable name as the first element
立即想到的一件事是程序参数包含可执行文件名称作为第一个元素
argv[0]
is "sjf"
argv[0]
是“sjf”
argv[1]
is "file.text"
argv[1]
是“文件.文本”
so you should be using
所以你应该使用
fr = fopen (argv[1], "r");
Remember when debugging to always try and narrow the problem down, if you know the location of the error the cause often becomes obvious or at least investigatable.
请记住,在调试时始终尝试缩小问题的范围,如果您知道错误的位置,原因通常会变得明显或至少可以调查。
In this case you should check argc >= 2
, print out argv[1]
to ensure you are trying to open the right file, then also check that the file was opened successfully.
在这种情况下,您应该检查argc >= 2
,打印出来argv[1]
以确保您尝试打开正确的文件,然后还要检查文件是否已成功打开。
Finally check the fscanf error codes to see that fscanf was able to read the number.
最后检查 fscanf 错误代码以查看 fscanf 能够读取数字。
回答by Adam Rosenfield
argv[0]
is the name of the program (./sjf
in your case), so you're trying to read in your own program's executable. Use argv[1]
instead to get the first real program argument.
argv[0]
是程序的名称(./sjf
在您的情况下),因此您正在尝试读取自己程序的可执行文件。使用argv[1]
来获取第一个真正的程序参数。
回答by Roland Illig
Your code looks clear and straight-forward, but there is one important thing missing: error handling.
您的代码看起来清晰而直接,但缺少一件重要的事情:错误处理。
What happens if the file you want to open does not exist? fopen returns NULL in that case.
如果要打开的文件不存在会怎样?在这种情况下,fopen 返回 NULL。
What happens if the file does not start with a number? fscanf returns the number of fields that have been successfully read, so you should check that the return value is at least 1.
如果文件不以数字开头会怎样?fscanf 返回已成功读取的字段数,因此您应该检查返回值是否至少为 1。
You need to somehow handle these cases, probably by printing some error message and exiting the program. When you do that, be sure to include the relevant information in the error messages. Then you will find the bug that the other answers have already mentioned.
您需要以某种方式处理这些情况,可能是通过打印一些错误消息并退出程序。执行此操作时,请确保在错误消息中包含相关信息。然后你会发现其他答案已经提到的错误。