gpt4 book ai didi

c - 为什么我的编辑器无法识别我的 typedef 结构? C

转载 作者:行者123 更新时间:2023-11-30 17:45:28 26 4
gpt4 key购买 nike

新问题:我必须将文件中的数据读入结构数组,并且我的 scanf 函数出现错误。我真的不确定扫描结构的细节。这是我写的:

#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++;
}

最佳答案

#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;
}

在此处编译。

关于c - 为什么我的编辑器无法识别我的 typedef 结构? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19622218/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com