C 中的 fscanf 分段错误
我正在尝试从文件中读取值并将它们存储在结构中。
该结构包含
char region
char country[100]
int country_code
该结构的实例称为 c[100]
我试图读取的文件看起来像这样
Europe
0 France
1 England
2 Germany
有一个未知的国家/地区数量,因此它一直读取到 EOF。
我创建了一个结构数组。
到目前为止,我的代码如下所示:
fp= fopen(countries,"r");
while (!feof(fp))
{
fscanf(fp, "%[^\n]", c.region);
while (!feof(fp))
{
fscanf(fp, "%d, %[^\n]", c[i].country_code, c[i].country);
i++;
}
}
我遇到分段错误。我确信我错过了或做错了一些明显的事情,但我不确定是什么,如果有人可以提供帮助,我将不胜感激。
I am trying to read in values from a file and store them in a structure.
The structure contains
char region
char country[100]
int country_code
The instance of this structure is called c[100]
The file i am trying to read in looks like this
Europe
0 France
1 England
2 Germany
There are an unkonwn number of countries, so it has keep reading until EOF.
I have created an array of the structures.
The code i have so far looks like this:
fp= fopen(countries,"r");
while (!feof(fp))
{
fscanf(fp, "%[^\n]", c.region);
while (!feof(fp))
{
fscanf(fp, "%d, %[^\n]", c[i].country_code, c[i].country);
i++;
}
}
I get a segmentation fault. I'm sure its something obvious that ive missed out or done wrong, but im not sure what, and i would be grateful if anyone could help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
检查返回值是否有errrs
需要传入c.region的地址:
&c.region
。但是,这仍然是错误的,因为您只分配了一个字符,并且 fscanf 会读取字符直到出现不匹配为止。您应该将 c.region 的声明更改为字符数组c.region[[00]
或其他内容。另外, c 是一个数组,而不是一个结构,所以我认为这不是您实际使用的代码。您的意思是 c[0].region 吗?
你还应该担心阅读的内容会超出你分配的内容。阅读 fscanf 限制其在传递的地址中存储内容的方法
在哪里将 i 设置为零?
同样,您需要传递 Country_code 字段的地址
&c[i].country_code"
。请注意,您不需要在国家/地区字段上使用 & 运算符,即使其他字段到目前为止的答案说你这样做,因为国家是一个字符数组,所以 c[i].country 与 &c[i].country 相同,如果文件中的行数多于 c[i] 中分配的条目,会发生什么] 大批?
Check the return value for errrs
You need to pass the address of c.region:
&c.region
. However, it's still wrong as you're only allocating one character and fscanf will read characters until a non-match. You should change the declaration of c.region to be a character arrayc.region[[00]
or something.Also, c is an array, not a struct, so I don't think this is the code you're actually using. Did you mean c[0].region?
You should also worry about reading more than whatever you have allocated. Read up on fscanf's ways of limiting what it stores in the address passed
Where do you set i to zero?
Again, you need to pass the address of the country_code field
&c[i].country_code"
. Note that you do not need to use the & operator on the country field, even though the other answers so far say you do as country is a char array and so c[i].country is the same as &c[i].countryWhat happens if there are more lines in the file than allocated entries in the c[i] array?
您需要获取country_code和country字段的地址:
否则fscanf会将这两个整数值解释为指针,并尝试将数据存储到其中=>分段错误。
You need to take the address of the country_code and country fields:
Otherwize fscanf will intepret those two integer values as pointers, and try storing data into them => segmentation fault.
使用类似它应该工作的结构,
例如。
with struct like
it should work eg.
应固定为
fscanf 需要一个地址来写入数据。您不需要对 char 数组使用
&
,因为它已经是一个指针。另外,在您的结构中
charregion;
应更改为charregion[100];
因为您没有该区域的一个字符,而是多个字符,IOW 是一个字符串。should be fixed to
as fscanf needs an address to write data. You do not need to use
&
for char array, as it is already a pointer.Also, in your structure
char region;
should be changed tochar region[100];
as you have not the one char for the region, but several ones, IOW a string.