Can Someone tell me what's wrong with this code?

EDIT: it's now working, thanks to emailbitesmyass and rgov. Here is the working code, if anyone was curious.

#include <stdio.h> #include <string.h>

int main()

{

    int n=0, dummyscore[3];
    char id[3], dummy[3][17];
    FILE *input = fopen("gradebook.dat","r");
    while(strcmp(id,"ZZ") != 0)
    {
            n++;
            fscanf(input,"%2s %s %s %d %d %d",id, dummy[0], dummy[1], &dummyscore[0], &dummyscore[1], &dummyscore[2]);
    }
    printf("%d \n", n);
    fclose(input);
    return 0;

}

Edit: The code with changes, I am now getting a segmentation fault instead. Also, one line in gradebook.dat looks like this: ' tb gottfried leibnitz 64 75 96 '

#include <stdio.h> #include <string.h>

int main()

{

    int n=0;
    char id[3], dummy[3][16];
    FILE *input = fopen("gradebook.dat","r");
    while(strcmp(id,"ZZ") != 0)
    {
            n++;
            fscanf(input,"%s %s %s %d %d %d",id, dummy[0], dummy[1], dummy[2][0], dummy[2][1], dummy[2][2]);
    }
    printf("%d",n);
    fclose(input);
    return 0;

}

Old Code:

#include <stdio.h> #include <string.h>

int main()

{

    int n=0;
    char id[2], dummy[3][16];
    FILE *input = fopen("gradebook.dat","r");
    while(id != "ZZ")
    {
            n++;
            fscanf(input,"%s %s %s %d %d %d",&id, &dummy[0], &dummy[1], &dummy[2][0], &dummy[2][1], &dummy[2][2]);
    }
    printf("%d",n);
    return 0;

}

I'm trying to count the lines in a dat file that ends with a line only containing the characters ZZ. It compiles, but when I run it, it doesn't print anything, and just lets me type things and press enter until I stop its run.

Posted Mar 24, 2012, 9:49 PM UTC · edited

010
Loading comments…