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

#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); }

2005-06-26 11:06
天使预备役
Rank: 2
等 级:论坛游民
威 望:3
帖 子:670
专家分:10
注 册:2004-4-6
收藏
得分:0 
studentType[i]这是结构变量,而&amp;p是结构变量的指针地址!
.和-&gt;怎么用看看!

差点把你忘了...
2005-07-07 10:30
快速回复:[求助] 遇到一个奇怪的事 求教
数据加载中...
 
   



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

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