[英]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. 新问题:我必须将文件中的数据读入结构数组中,而scanf函数却出现错误。 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). 请注意,尽管
_t
后缀在POSIX系统(如Mac OS X和Linux)上保留。 It's better to either use _type
instead, or no suffix at all. 最好改用
_type
或根本不加后缀。
Your problems though (the compilation errors) lie elswhere. 但是,您的问题(编译错误)仍然存在。 First, you're using
scanf()
instead of fscanf()
. 首先,您使用的是
scanf()
而不是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. scanf()
始终从stdin
流中读取并且没有FILE*
参数(因此,当您尝试将FILE*
作为第一个参数传递时,编译器将其视为错误,因为它始终期望使用const char*
。)从您自己的文件流中,您需要使用fscanf()
,它确实带有FILE*
参数。 Eg: 例如:
fscanf(ifile, "%lu", &runarray[i].bibnum);
Note: it's %lu
, not %ul
.) 注意:它是
%lu
,不是%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: 由于
runner_t.lastname
已经是一个指针,因此可以runner_t.lastname
传递它:
fscanf(ifile, "%s", runarray[i].lastname);
Furthermore, you misspelled runner_t.firstname
in the struct declaration. 此外,您在struct声明中拼写了
runner_t.firstname
。 You named it fistname
. 您将其命名为
fistname
。 Change that too. 也改变它。
Finally, in your printf()
, you use %d
as the format specifier to print bignum
. 最后,在
printf()
,使用%d
作为格式说明符来打印bignum
。 Since it's an unsigned long
, you need to use %lu
. 由于它是
unsigned long
,因此您需要使用%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:
ideone.com片段仅添加了此主要功能,并且运行良好:
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... 请参阅http://sscce.org ,以获取有关如何避免因提问失败而被压倒票的好建议。
After edit: use fscanf
, because scanf
does not take FILE*
as first argument (check documentation for both). 编辑后:使用
fscanf
,因为scanf
不会将FILE*
作为第一个参数(请查看两个文档)。 Always check return value of any scanf
family function. 始终检查任何
scanf
系列功能的返回值。 Also check return value of fopen
for errors. 还要检查
fopen
返回值是否有错误。
Additionally, do not use &
when passing array for fscanf
%s
. 此外,在为
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);
例如:
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. 在这里编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.