struct phone
{
char fname[20];
char lname[20];
char pnumber[20];
};
int main()
{
int x=0;
int n;
char s[100];
FILE * f;
f = fopen("phone book","r");
if (f != NULL)
{
printf("file exist !\n");
while(!(feof(f)))
{
fscanf(f,"%s,%s,%s",s[x].fname,s[x].lname,s[x].pnumber);
x++;
}
}
printf("1- add");
printf("2- query");
scanf("%d",&n);
if(n==1)
printf("%s",s[n].fname);
if(n==2)
fclose(f);
}
我编写此程序,fscanf
错误评论
'main':|
request for member 'fname' in something not a structure or union|
request for member 'lname' in something not a structure or union|
request for member 'pnumber' in something not a structure or union|
request for member 'fname' in something not a structure or union|
: variable 's' set but not used [-Wunused-but-set-variable]|
: control reaches end of non-void function [-Wreturn-type]|
||=== Build finished: 4 errors, 2 warnings (0 minutes, 0 seconds) ===|
答案 0 :(得分:1)
s
被声明为char s[100];
,因此s[x]
是char
而不是struct phone
。您需要将s
声明为struct phone s[100];
,并且您的第一个问题将会消失。
答案 1 :(得分:0)
错误
request for member 'fname' in something not a structure or union|
request for member 'lname' in something not a structure or union|
request for member 'pnumber' in something not a structure or union|
request for member 'fname' in something not a structure or union|
: variable 's' set but not used [-Wunused-but-set-variable]|
更改
char s[100]; // is not a structure. You need to declare it as structure
到
struct phone s[100];
和错误
control reaches end of non-void function [-Wreturn-type]|
作为main
的返回类型为int
(非void
函数,其返回类型不是void
),您需要添加return 0;
在}
的结束括号main
之前的陈述。
答案 2 :(得分:0)
好吧,编译器是对的。你宣布
char s[100];
并尝试访问其成员,就像它将是一个结构数组。它只是一个char数组。
我想你想做点什么
struct phone s[100];
这将声明一个包含100个struct phone的数组。
另外,请注意虽然(!feof(f))在这种情况下不建议使用:Why is “while ( !feof (file) )” always wrong?