[英]c - fscanf segmentation fault
Really strange problem with fscanf. fscanf真的很奇怪。 It seems as if it can't find the file.
似乎找不到文件。 Heres the code:
这是代码:
char obs_file[255];
FILE *obs_fp;
strcpy(obs_file, "/aber/dap/cetaceans/data/observers_1.txt");
obs_fp = fopen(obs_file, "r");
date_time t;
fscanf(obs_fp, "%d %d %d %d %d %d\n", &t.day, &t.mth, &t.yr, &t.hrs, &t.mns, &t.scs); //This line runs fine
obs_head.obs->time = t;
printf("%d %d %d %d %d %d\n", t.day, t.mth, t.yr, t.hrs, t.mns, t.scs);
while(feof(obs_fp) == 0) {
char id[5];
char a[7];
char b[7];
location loc;
double lng = 0.0, lat = 0.0;
fscanf(obs_fp, "%s %lf %lf", id, &lat, &lng); //Seg fault here on first run of loop
loc.lat = lat;
loc.lng = lng;
add_obs_node(make_obs_node(id, loc, t));
}
File to be read: 要读取的文件:
05 11 2014 14 53 00
AB01 52.408 -4.217
It seems like the file pointer has changed somewhere around the while statement, I would understand if I was reading over the end of file, but it fails while there are definitely lines left. 似乎文件指针在while语句周围的某个地方发生了变化,如果我正在读取文件末尾,我会理解的,但是在肯定还有行的情况下它失败了。 Also, I know Im opening the file right, as the first fscanf runs fine.
另外,我知道Im可以正确打开文件,因为第一个fscanf运行良好。
Any ideas? 有任何想法吗?
Wrong use of feof()
and unlimited fscanf("%s"...
错误使用了
feof()
和无限的fscanf("%s"...
feof()
reports if EOF occurred due to previous IO, not if it is about to occur. feof()
报告EOF是否是由于先前的IO而发生的,而不是是否即将发生。
Use instead 改用
char id[5];
double lng = 0.0, lat = 0.0;
while(fscanf(obs_fp, "%4s%lf%lf", id, &lat, &lng) == 3) {
loc.lat = lat;
loc.lng = lng;
add_obs_node(make_obs_node(id, loc, t));
}
I suspect original code failed on the 2nd iteration. 我怀疑原始代码在第二次迭代中失败。 Assume the last data in the file was
"AB01 52.408 -4.217\\n"
. 假设文件中的最后一个数据为
"AB01 52.408 -4.217\\n"
。 fscanf(obs_fp, "%s %lf %lf"
would scan up to the "\\n"
and put "\\n"
back into stdin
as it is not part of a double
. EOF flag is not set. The use of feof()
signals no EOF. So fscanf(obs_fp, "%s %lf %lf"
happens again, but no data is save in id
, as "%s"
consume leading white-space but has not non-white-space to save. Code does not check the fscanf()
return value (bad), but assumes good data in id
, which may be junk. Then add_obs_node()
is called with an invalid string id
. fscanf(obs_fp, "%s %lf %lf"
将扫描到"\\n"
并将"\\n"
放回stdin
因为它不是double
一部分。未设置EOF标志feof()
表示没有EOF。因此fscanf(obs_fp, "%s %lf %lf"
再次发生,但是id
没有保存任何数据,因为"%s"
消耗了前导空格,但没有非空格要保存。代码不会检查fscanf()
返回值(错误),而是假设id
数据add_obs_node()
可能是垃圾),然后使用无效的字符串id
调用add_obs_node()
。
Other failure mechanisms could have occurred too - need to see more code. 其他故障机制也可能发生过-需要查看更多代码。
Bottom line: Check fscanf()
results. 底线:检查
fscanf()
结果。 Limit string input. 限制字符串输入。
Minor: Note that the spaces between "%d %d"
are not needed, but OK to have. 次要:请注意,
"%d %d"
之间的空格不是必需的,但可以有空格。 The final "\\n"
is also OK but not needed. 最后的
"\\n"
也可以,但是不需要。 It is not simply consuming the following '\\n'
, but any and all following white-space. 它不只是消耗以下
'\\n'
,而且还消耗所有以下所有空格。
if (6 != fscanf(obs_fp, "%d%d%d%d%d%d",
&t.day, &t.mth, &t.yr, &t.hrs, &t.mns, &t.scs)) {
Handle_BadData();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.