0
#include <stdio.h>
#include <stdlib.h>
//#define true 0

typedef struct
{
    char currency[8];
    int exchangerate;
} randT;


void main()
{
    int i, num;
    char currency1[8], ch;
    FILE *file = fopen("address", "r");

    randT name[7];

    while(fscanf(file, "%i", &name[i].exchangerate) != EOF)/*I think this is where my problem is*/
    {
        fscanf(file, "%s %i", &name[i].currency, &name[i].exchangerate);
        //printf("%s %i\n", currency1, num);
        //if (fscanf(file, "%i", &currency1) == EOF) break;
        printf("%s %i\n", name[i].currency, name[i].exchangerate);
        i++;
    }

    fclose(file);
}

It is giving segmentation fault(core dumped) and i am fairly new to the fscanf functions and such. Please help! my text file looks like this: jeff 4 jina 5 jeffrey 6 jinna 7 jeffu 8 jinina 9 jeffz 10

Apple
  • 401
  • 1
  • 4
  • 7
  • 2
    Your seg fault is because you never initialize `i`. – Carey Gregory Jun 05 '14 at 01:24
  • Start by using `%d` instead of `%i` to read and print integers. You also need to learn how to debug given a segmentation fault (hint, read up on gdb, and try `gdb a.out core`). It will tell you which specific line went wrong. And why is `name` of type `randT`? – Scott Mermelstein Jun 05 '14 at 01:24
  • This is funny, your question seems a lot like http://stackoverflow.com/questions/24049510/c-read-and-store-data-file-for-further-calculations/24050061?noredirect=1#comment37080678_24050061 – Leonardo Jun 05 '14 at 01:24

1 Answers1

1

The cause of the crash is that you need to initialize int i = 0;. As you have it, i is indeterminate, so probably name[i] is a massively out-of-bounds access.

For the fscanf, you have mostly the right idea about the loop, except instead of checking != EOF it's better to check == 1 or however many items you were reading. If the file contains something that can't be processed as a number, then fscanf will not return EOF.

However this is not the right way to read your particular file. You read %i then %s %i every time around, but your file only contains a string and an int. Change to:

while ( 2 == fscanf(file, "%7s %i", name[i].currency, &name[i].exchangerate) )

Also after doing i++ you need to check that i has not gone out of bounds (i.e. it's less than 7).

Note that name[i].currency decays to pointer to char - you shouldn't put & on it. Also you should use the length specifier to avoid running over the end of the array.

I would use the loop structure:

for (num_read = 0; num_read < sizeof name / sizeof name[0]; ++num_read)
{
     if ( 2 != fscanf(file,.....
         break;

     printf(....
}
M.M
  • 138,810
  • 21
  • 208
  • 365
  • can you explain why name[i].currency decays from pointer to char? – Apple Jun 05 '14 at 01:57
  • @user3580389 `name[i].currency` is an array and therefore a pointer. It does not decay to anything. – Carey Gregory Jun 05 '14 at 02:00
  • "Note that name[i].currency decays to pointer to char - you shouldn't put & on it." Also, I tried "while ( 2 == fscanf(file, "%7s %i", name[i].currency, &name[i].exchangerate) )" and printing everything out but it seems to skip over some items? – Apple Jun 05 '14 at 02:03
  • In C, arrays have different behaviour to other variables. If you use an array when a value was expected then it decays to a pointer to the first element. So , despite the fact that `name[i].currency` is an array, the `fscanf` function sees a pointer to the first character of the array (which is exactly what it wants). – M.M Jun 05 '14 at 02:03
  • Also, I tried "while ( 2 == fscanf(file, "%7s %i", name[i].currency, &name[i].exchangerate) )" and printing everything out but it seems to skip over some items? – Apple Jun 05 '14 at 02:57
  • post your actual code, plus the input file, plus the output you are getting. (updating the question with the new stuff at the end might be best, or start a new question) – M.M Jun 05 '14 at 03:19