C 中的 fscanf 分段错误

发布于 2024-10-08 02:23:12 字数 618 浏览 3 评论 0原文

我正在尝试从文件中读取值并将它们存储在结构中。

该结构包含

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世蒼涼 2024-10-15 02:23:17
 myfile = fopen(countries,"r");

检查返回值是否有errrs

while (!feof(myfile))
{
fscanf(myfile, "%[^\n]", c.region);

需要传入c.region的地址:&c.region。但是,这仍然是错误的,因为您只分配了一个字符,并且 fscanf 会读取字符直到出现不匹配为止。您应该将 c.region 的声明更改为字符数组 c.region[[00] 或其他内容。

另外, c 是一个数组,而不是一个结构,所以我认为这不是您实际使用的代码。您的意思是 c[0].region 吗?

你还应该担心阅读的内容会超出你分配的内容。阅读 fscanf 限制其在传递的地址中存储内容的方法

在哪里将 i 设置为零?

    while (!feof(myfile))
    {
    fscanf(myfile, "%d, %[^\n]", c[i].country_code, c[i].country);

同样,您需要传递 Country_code 字段的地址 &c[i].country_code" 。请注意,您不需要在国家/地区字段上使用 & 运算符,即使其他字段到目前为止的答案说你这样做,因为国家是一个字符数组,所以 c[i].country 与 &c[i].country 相同,

    i++;

如果文件中的行数多于 c[i] 中分配的条目,会发生什么] 大批?

 myfile = fopen(countries,"r");

Check the return value for errrs

while (!feof(myfile))
{
fscanf(myfile, "%[^\n]", c.region);

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 array c.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?

    while (!feof(myfile))
    {
    fscanf(myfile, "%d, %[^\n]", c[i].country_code, c[i].country);

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].country

    i++;

What happens if there are more lines in the file than allocated entries in the c[i] array?

最冷一天 2024-10-15 02:23:17

您需要获取country_code和country字段的地址:

fscanf(myfile, "%d, %[^\n]", &c[i].country_code, &c[i].country); 

否则fscanf会将这两个整数值解释为指针,并尝试将数据存储到其中=>分段错误。

You need to take the address of the country_code and country fields:

fscanf(myfile, "%d, %[^\n]", &c[i].country_code, &c[i].country); 

Otherwize fscanf will intepret those two integer values as pointers, and try storing data into them => segmentation fault.

太阳男子 2024-10-15 02:23:17

使用类似它应该工作的结构,

char region[100]
char country[100]
int country_code

例如。

char aregion[100]="", line[100];
...
while( fgets(line,100,myfile) )
{
  if( *aregion && 2==sscanf(line,"%d%99[^\n]",&c[i].country_code,c[i].country) )
    strcpy(c[i++].region,aregion);
  else
  if( !strchr(line,' ') )
    sscanf(line,"%99[^\n]",aregion);
}

with struct like

char region[100]
char country[100]
int country_code

it should work eg.

char aregion[100]="", line[100];
...
while( fgets(line,100,myfile) )
{
  if( *aregion && 2==sscanf(line,"%d%99[^\n]",&c[i].country_code,c[i].country) )
    strcpy(c[i++].region,aregion);
  else
  if( !strchr(line,' ') )
    sscanf(line,"%99[^\n]",aregion);
}
时光清浅 2024-10-15 02:23:16
fscanf(myfile, "%d, %[^\n]", c[i].country_code, c[i].country);

应固定为

fscanf(myfile, "%d, %[^\n]", &(c[i].country_code), c[i].country);

fscanf 需要一个地址来写入数据。您不需要对 char 数组使用 &,因为它已经是一个指针。

另外,在您的结构中 charregion; 应更改为 charregion[100]; 因为您没有该区域的一个字符,而是多个字符,IOW 是一个字符串。

fscanf(myfile, "%d, %[^\n]", c[i].country_code, c[i].country);

should be fixed to

fscanf(myfile, "%d, %[^\n]", &(c[i].country_code), c[i].country);

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 to char region[100]; as you have not the one char for the region, but several ones, IOW a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文