fscanf in C segmentation error

14,007

Solution 1

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.

Solution 2

 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?

Share:
14,007
user476145
Author by

user476145

Updated on June 04, 2022

Comments

  • user476145
    user476145 almost 2 years

    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.

  • Vladimir Ivanov
    Vladimir Ivanov over 13 years
    c[i].country is already a char array, no & here is necessary.
  • John Källén
    John Källén over 13 years
    You're right that there is no need for & for country. However it is not a char, it is an array of char (char[100]). When passing an array of char as a parameter to a function, the compiler converts it into a char pointer (char*).
  • user411313
    user411313 over 13 years
    -1 comma in formatspec is wrong here, fscanf-return are ignored