当前位置: 代码迷 >> C语言 >> [求助] 遇到一个奇怪的事 求教
  详细解决方案

[求助] 遇到一个奇怪的事 求教

热度:339   发布时间:2005-06-26 11:08:00.0
[求助] 遇到一个奇怪的事 求教

#include<stdio.h> #include<iostream.h> #include<windows.h>

struct Student { int id; char name[30]; char sex; int score; }; void main() { FILE *fp; struct Student studentType[2],*p=studentType; int i; if((fp=fopen("d:\\thybare","wb"))==NULL) { printf("can not open file:\n"); exit(1); } for(i=0;i<2;i++) //如用指针 for(i=0;i<2;i++,p++) { printf("enter student id\n"); scanf("%d",&studentType[i].id); //为什么这里改成&p->id就 运行不行啊 fflush(stdin); printf("enter student name\n"); scanf("%s",studentType[i].name); //为什么这里改成p->name就 运行不行啊 fflush(stdin); printf("enter student sex\n"); scanf("%c",&studentType[i].sex); //为什么这里改成&p->sex就 运行不行啊 printf("enter student score\n"); scanf("%d",&studentType[i].score); ////为什么这里改成p->score就 运行不行啊 } if((fwrite(p,sizeof(struct Student),2,fp))!=2) { printf("can not write file:"); exit(1); } fclose(fp); if((fp=fopen("d:\\thybare","rb"))==NULL) { printf("can not open file:"); exit(1); } p=studentType; for(i=0;i<2;i++,p++) { if(fread(p,sizeof(struct Student),1,fp)==1) printf("%d\n%s\n%c\n%d\n",p->id,p->name,p->sex,p->score); else printf("can not read the file:"); } fclose(fp); }

搜索更多相关的解决方案: void  include  file  

----------------解决方案--------------------------------------------------------
前面的定义*P=STUDENTTYPE  应该写成*P=STUDENTTYPE[ ] 吧
----------------解决方案--------------------------------------------------------
我上面的程序是正确的啊 ,输出的时候用的就是p-&gt;啊
所以*P=studentType应该对的吧?

不过如果改成旁边的注释就不对了
----------------解决方案--------------------------------------------------------

#include<stdio.h> //#include<iostream.h> #include<windows.h>

struct Student { int id; char name[30]; char sex; int score; };

void main() { FILE *fp; struct Student studentType[2],*p=studentType; int i; if((fp=fopen("d:\\thybare","wb"))==NULL) { printf("can not open file:\n"); exit(1); } //for(i=0;i<2;i++) //如用指针 for(i=0;i<2;i++,p++) for(i=0;i<2;i++,p++) { printf("enter student id\n"); //scanf("%d",&studentType[i].id); //为什么这里改成&p->id就 运行不行啊 scanf("%d",&p->id); printf("%d\n",p->id); fflush(stdin); printf("enter student name\n"); //scanf("%s",studentType[i].name); //为什么这里改成p->name就 运行不行啊 scanf("%s",p->name); fflush(stdin); printf("enter student sex\n"); //scanf("%c",&studentType[i].sex); //为什么这里改成&p->sex就 运行不行啊 scanf("%c",&p->sex); fflush(stdin); printf("enter student score\n"); //scanf("%d",&studentType[i].score); ////为什么这里改成p->score就 运行不行啊 scanf("%d",&p->score); fflush(stdin); } ////////////////////////////////////////////////////////////// p=studentType; ///////////////////////////////////////////////////////////// if((fwrite(p,sizeof(struct Student),2,fp))!=2) { printf("can not write file:"); exit(1); } fclose(fp); if((fp=fopen("d:\\thybare","rb"))==NULL) { printf("can not open file:"); exit(1); } //p=studentType; for(i=0;i<2;i++,p++) { if(fread(p,sizeof(struct Student),1,fp)==1) printf("%d\n%s\n%c\n%d\n",p->id,p->name,p->sex,p->score); else printf("can not read the file:"); } fclose(fp); }


----------------解决方案--------------------------------------------------------
exit(-1),studentType后面不应有长度了吧。

[此贴子已经被作者于2005-6-27 17:05:44编辑过]



----------------解决方案--------------------------------------------------------
  相关解决方案