望各位大侠指导下,小弟在此万分感谢。。。。
student.c 文件内容如下:#include <stdio.h>
#include <stdlib.h>
struct student {
char name[20];
int num;
double score;
struct student *next;
};
int main()
{
struct student *create_list(int num);
int num = 6;
struct student *q;
q = create_list(num);
return 0;
}
struct student *create_list(int num)
{
FILE *fp;
struct student *list;
struct student *head;
struct student *q;
head = (struct student *)malloc(sizeof(struct student));
head->next = NULL;
p = head;
fp = fopen("file","w");
if(fp == NULL)
{
printf(" 打开文件失败 \n");
exit(-1);
}
while(num > 0)
{
list = (struct student *)malloc(sizeof(struct student));
printf(" 输入学生姓名 \n");
scanf("%s",list->name);
printf(" 输入学生学号 \n");
scanf("%d",&list->num);
printf(" 输入学生成绩 \n");
scanf("%lf",&list->score);
fwrite(list,sizeof(struct student),1,fp);
p->next = list;
list = p;
num--;
}
fclose(fp);
return head;
}
此程序是把学生的相关信息写入文件 file 中
rdfile.c 文件内容如下:
#include <stdio.h>
#include <string.h>
struct student {
char name[20];
int num;
double score;
struct student *next;
};
int main()
{
struct student list;
FILE *fp;
fp = fopen("file","r");
if(fp == NULL)
{
printf(" 打开文件失败 \n");
exit(-1);
}
while(feof(fp) != 0)
{
fread((struct student *)&list,sizeof(struct student),1,fp);
printf("%s",list.name);
printf("%d",list.num);
printf("%lf",list.score);
}
fclose(fp);
return 0;
}
此程序意在读取 student.c(上个程序)写入文件的内容。 。。。。 希望各位大侠指导下这个读程序为什么失败,小弟万分感谢。。。。。
[ 本帖最后由 华夏永荣 于 2012-5-29 21:10 编辑 ]