简体   繁体   中英

Why isn't my editor recognizing my typedef struct? C

New question: I have to read in data from files into an array of structures, and I'm getting errors with my scanf function. I'm really unsure of the details of scanning into structures. This is what I have written:

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

#define runnum 500
#define charnum 20

typedef struct {
        unsigned long bibnum;
        char lastname[charnum];
        char fistname[charnum];
        int grade;
        char team[charnum];
        char state[charnum];
        int time1;
        float time2;
    } runner_t;

int main(void)
{
    int i;
    FILE *ifile, *jfile;

    ifile = fopen("20121006.boys.txt", "r");
    jfile = fopen("20121006.girls.txt", "r");

    runner_t runarray[runnum]; 

    i = 0;

    while  (i < runnum)
        {
            scanf(ifile, "%ul", &runarray[i].bibnum);
            scanf(ifile, "%s", &runarray[i].lastname);
            scanf(ifile, "%s", &runarray[i].firstname);
            scanf(ifile, "%d", &runarray[i].grade);
            scanf(ifile, "%s", &runarray[i].team);
            scanf(ifile, "%s", &runarray[i].state);
            scanf(ifile, "%d", &runarray[i].time1);
            scanf(ifile, "%f", &runarray[i].time2);
        printf("%d %s %s %d %s %s %d:%f", runarray[i].bibnum, runarray[i].lastname, runarray[i].firstname, runarray[i].grade, runarray[i].team, runarray[i].state, runarray[i].time1, runarray[i].time2); 
        i++;
     }

The editor problem is purely cosmetic. You can try this instead:

typedef struct runner_t {
    // ...
} runner_t;

Note though that the _t suffix is reserved on POSIX systems (like Mac OS X and Linux). It's better to either use _type instead, or no suffix at all.

Your problems though (the compilation errors) lie elswhere. First, you're using scanf() instead of fscanf() . scanf() always reads from the stdin stream and has no FILE* argument (so the compiler sees it as an error when you try to pass a FILE* as the first argument, since it always expects a const char* instead.) To read from your own file streams, you need to use fscanf() , which does take a FILE* argument. Eg:

fscanf(ifile, "%lu", &runarray[i].bibnum);

Note: it's %lu , not %ul .)

Also note that when you read in the strings, you're passing pointers to them even though they're already pointers:

fscanf(ifile, "%s", &runarray[i].lastname);

Since runner_t.lastname is already a pointer, just pass it as-is:

fscanf(ifile, "%s", runarray[i].lastname);

Furthermore, you misspelled runner_t.firstname in the struct declaration. You named it fistname . Change that too.

Finally, in your printf() , you use %d as the format specifier to print bignum . Since it's an unsigned long , you need to use %lu .

Your struct code is fine, see this if you don't believe it otherwise. That ideone.com snippet adds just this main function, and works fine:

int main(void) {
  runner_t foo;
  printf ("sizeof foo is %zu\n", sizeof foo);
  return 0;
}

See http://sscce.org for good advice on how to avoid getting downvotes for badly asked question...


After edit: use fscanf , because scanf does not take FILE* as first argument (check documentation for both). Always check return value of any scanf family function. Also check return value of fopen for errors.

Additionally, do not use & when passing array for fscanf %s . Name of array means address of array, and taking & of that makes no sense, and is not allowed. So for example: fscanf(ifile, "%s", runarray[i].state);

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

#define RUNNUM 500
#define CHARNUM 20

typedef struct {
        unsigned long bibnum;
        char lastname[CHARNUM];
        char firstname[CHARNUM];
        int grade;
        char team[CHARNUM];
        char state[CHARNUM];
        int time1;
        float time2;
    } runner_t;

int main(void)
{
    int i;
    FILE *ifile, *jfile;

    ifile = fopen("20121006.boys.txt", "r");
    jfile = fopen("20121006.girls.txt", "r");

    runner_t runarray[RUNNUM];

    for (i=0; i < RUNNUM; i++)
        {
            fscanf(ifile, "%lu", &runarray[i].bibnum);
            fscanf(ifile, "%s", runarray[i].lastname);
            fscanf(ifile, "%s", runarray[i].firstname);
            fscanf(ifile, "%d", &runarray[i].grade);
            fscanf(ifile, "%s", runarray[i].team);
            fscanf(ifile, "%s", runarray[i].state);
            fscanf(ifile, "%d", &runarray[i].time1);
            fscanf(ifile, "%f", &runarray[i].time2);
        printf("%lu %s %s %d %s %s %d:%f"
        , runarray[i].bibnum
        , runarray[i].lastname, runarray[i].firstname
        , runarray[i].grade, runarray[i].team
        , runarray[i].state
        , runarray[i].time1, runarray[i].time2);

     }
return 0;
}

Compiles here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM