| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 593 人关注过本帖
标题:[求助] 遇到一个奇怪的事 求教
只看楼主 加入收藏
编程无限
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-5-7
收藏
 问题点数:0 回复次数:4 
[求助] 遇到一个奇怪的事 求教

#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 
2005-06-26 11:08
wyak47
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2005-6-26
收藏
得分:0 
前面的定义*P=STUDENTTYPE  应该写成*P=STUDENTTYPE[ ] 吧
2005-06-26 16:10
编程无限
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2005-5-7
收藏
得分:0 
我上面的程序是正确的啊 ,输出的时候用的就是p-&gt;啊
所以*P=studentType应该对的吧?

不过如果改成旁边的注释就不对了

2005-06-26 18:39
musicml
Rank: 1
等 级:新手上路
帖 子:273
专家分:0
注 册:2005-4-2
收藏
得分: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++) 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); }


Every thing is possible.
2005-06-26 23:11
weizheng
Rank: 1
等 级:新手上路
威 望:2
帖 子:286
专家分:0
注 册:2005-4-21
收藏
得分:0 
exit(-1),studentType后面不应有长度了吧。

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



温和如玉,完美纯正。
2005-06-27 17:01
快速回复:[求助] 遇到一个奇怪的事 求教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.024460 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved