0
struct athlete{
    char firstName[50];
    char lastName[70];
    int age, total, bronze, gold, silver, year;
    char country[70];
    char sport[100];
};

I have a structure to store olympic athletes name medals country age etc. from a give file. A. J. Mleczko 26 United States 2002 Ice Hockey 0 1 0 1

Aaron Armstrong 30  Trinidad and Tobago 2008    Athletics   0   1   0   1
Aaron Egbele    25  Nigeria 2004    Athletics   0   0   1   1
Aaron Gate  21  New Zealand 2012    Cycling 0   0   1   1

so what I was doing was this:

void readFile(){
    FILE *fptr;
    int i;
    fptr = fopen("olympics.txt", "r");
    if (fptr == NULL)
    {
        printf ("Error");
    }
    i = 0;
    while (!feof(fptr))
    {
        fscanf(fptr,"%s %s %d %s %d %s %d %d %d %d ", athletes[i].firstName,athletes[i].lastName, &athletes[i].age, athletes[i].country, &athletes[i].year, athletes[i].sport, &athletes[i].gold, &athletes[i].silver, &athletes[i].bronze, &athletes[i].total);
        i++;
    }
}

Everytime I run it the program just crashes.I'm trying to run a simple search also so that might be a problem.

void compareString(char first[], char second[]) {
    int i = 0;
    struct athlete athletes[2000];
    for(i = 0; i < 2000; i++)
    {
        if(strcmp(athletes[i].firstName, first) == 0 && strcmp(athletes[i].lastName, second) == 0 )
        {
            printf("%s%s", athletes[i].firstName, athletes[i].lastName);
        }
        else
        {
            printf("error");
        }
    }
}
nalzok
  • 14,965
  • 21
  • 72
  • 139
conk
  • 57
  • 1
  • 1
  • 9
  • 2
    `New Zealand`, among others, is not a single string. Your format specifiers go out of sync due to that. – Unimportant Apr 06 '16 at 18:57
  • Oh I see that, thanks :D – conk Apr 06 '16 at 18:58
  • Just a note, 'the program just crashes' is useless. When, with what message, etc., etc. would be helpful. Also, the answer to this question would be readily apparent if you single stepped it in a debugger – KevinDTimm Apr 06 '16 at 19:07
  • 1
    What do you think what will happen if the file`"olympics.txt"` cannot be opened? – Jabberwocky Apr 06 '16 at 19:07
  • And did you use a debugger ? – Jabberwocky Apr 06 '16 at 19:09
  • well I'm pretty new to this and am not really sure how to use the debugger to find the specific answer to this – conk Apr 06 '16 at 19:11
  • User input is very error-prone, even if the programmer is the only user... Testing the return vaue of `fopen()` is good. The return value of `fscanf()` can be tested as well (it is equal to he number of items successfully read). Lastly `char st[42];fscanf(fp,"%s",st);` can lead to a segmentation fault (or even worse...that's undefined behavior) if the string in the file is too long. Use `char st[42];fscanf(fp,"%41s",st);` instead. – francis Apr 06 '16 at 19:48
  • Also, `while (!feof(fptr)) {...}` won't do what you think. See [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Andrew Henle Apr 06 '16 at 20:53

1 Answers1

0

"Trinidad and Tobago" and "New Zealand" contain spaces, which prevents %s from scanning them correctly. Also, it's not a good idea to use while(!feof(fptr)).

You can use the following loop to input data:

while (fscanf(fptr," %s %s %d %[^0-9] %d %[^0-9] %d %d %d %d", athletes[i].firstName, athletes[i].lastName, &athletes[i].age, athletes[i].country, &athletes[i].year, athletes[i].sport, &athletes[i].gold, &athletes[i].silver, &athletes[i].bronze, &athletes[i].total) == 10)
{
    i++;
}
nalzok
  • 14,965
  • 21
  • 72
  • 139