首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

书上例子有有关问题

2013-03-27 
书上例子有问题求助!#includestdio.h#include stdlib.hvoid main(){FILE *fpchar ch,st[20]int ist

书上例子有问题求助!
#include<stdio.h>
#include <stdlib.h>
void main()
{FILE *fp;
 char ch,st[20];
 int i;
 struct stu
 { char name[15];
  char num[6];
  float score;
 }student;
 if((fp=fopen("file2.txt","w"))==NULL)
 { printf("this file Cannot be opened!");
  exit(1);
 }
 printf("input name:\n");
 scanf("%s",name);
 printf("input num:\n");
 scanf("%s",num);
 printf("input score:\n");
 scanf("%f",&score);
 fprintf(fp,"%s%s%7.2f\n",student.name,student.num,student.score);
fclose(fp);
if((fp=fopen("file2.txt","r"))==NULL)
{ printf("This file Cannot be opened!");
  exit(0);
}
printf("output from file:\n");
while(fscanf(fp,"%s%s%f\n",student.name,student.num,student.score)!=EOF)
printf("%s%s%7.2f\n",student.name,student.num,student.score);
fclose(fp);
}


--------------------Configuration: helloc - Win32 Debug--------------------
Compiling...
helloc.cpp
D:\VC语言\helloc\helloc.cpp(17) : error C2065: 'name' : undeclared identifier
D:\VC语言\helloc\helloc.cpp(19) : error C2065: 'num' : undeclared identifier
D:\VC语言\helloc\helloc.cpp(21) : error C2065: 'score' : undeclared identifier
执行 cl.exe 时出错.


这怎么还要定义呢?

[解决办法]

探讨

printf("input name:\n");
scanf("%s",student.name);

[解决办法]
要定义初始化结构体stu student,使用scanf("%s",student.name);

热点排行