【发布时间】:2015-01-07 18:27:08
【问题描述】:
这个程序在我运行时返回“Segmentation fault: 11”。编译器 (GCC) 不会返回任何错误或警告。
#include <stdio.h>
#include <string.h>
typedef struct {
char day[3];
char month[3];
char year[5];
} DATA;
DATA *data;
int main()
{
FILE *file;
char line_buffer[BUFSIZ];
if (!(file = fopen("file.dat", "rt")))
{
printf ("Something went wrong while opening the file.\n");
}
else
{
int line_number = 0;
while (fgets(line_buffer, sizeof(line_buffer), file))
{
++line_number;
if (line_number == 1) { strncpy(data->day, line_buffer, 2); }
else if (line_number == 2) { strncpy(data->month, line_buffer, 2); }
else if (line_number == 3) { strncpy(data->year, line_buffer, 4); }
}
printf("Content: %s-%s-%s\n", data->day, data->month, data->year);
}
return 0;
}
file.dat 的内容是:
12
08
1990
我已经用 GDB 调试过了,结果如下:
(gdb) run
Starting program: /Users/macuser/Desktop/Primitiva/Proyecto/a.out
Program received signal SIGSEGV, Segmentation fault.
0x00007fff949413a0 in _dispatch_queue_attrs () from /usr/lib/system/libdispatch.dylib
这是什么意思,我可以做些什么来解决这个问题?谢谢!
【问题讨论】:
标签: c gcc segmentation-fault