0

我在代码的第一行遇到了一个奇怪的分段错误。

我所做的就是打电话

FILE *src = fopen(argv[1], 'r');

我在 gdb 中的消息出现 seg 错误...

程序收到信号 SIGSEGV,分段错误。

0x00007ffff779956d in _IO_file_fopen () from /lib/x86_64-linux-gnu/libc.so.6

我将文件名直接复制到运行时执行中。想法?

4

3 回答 3

14

的第二个参数fopen()应该是一个字符串,而不是char

FILE *src = fopen(argv[1], "r");

注意双引号。

打开编译器警告并密切关注它们总是一个好主意。我的编译器选择了不正确的参数:

test.c:4:1: warning: passing argument 2 of 'fopen' makes pointer from integer without a cast [enabled by default]
In file included from test.c:1:0:
/usr/include/stdio.h:250:7: note: expected 'const char *' but argument is of type 'int'
于 2013-04-08T13:02:21.463 回答
0

可能传递的字符串不是以 null 结尾的,或者您正在访问 argv 上的无效索引

于 2013-04-08T13:02:06.583 回答
0

代替

FILE *src = fopen(argv[1], 'r');

你需要写

FILE *src = fopen(argv[1], "r");
于 2013-04-08T14:46:41.437 回答