fread函数将磁盘文件读取到一个链表中。函数如下,希望能获得正确代码,就是将readfile0这个函数改下。谢
#include<stdio.h>
#include<math.h>
#include<malloc.h>
#include<stdlib.h>
#include<string.h>
enum 易做图{female,male};
char number[8];
struct student* Bubble1(struct student* head,int i);\\链表的冒泡排序
struct student
{
char name[20];
enum 易做图 易做图;
int score[3];
char number[8];
struct student *next;
};
void writefile0(struct student*head) //已知链表头head,将之写入文件中
{
struct student*p1;
//struct student*head;
FILE *f0;
char string[40];
printf("请输入文件路径或者文件名字!(扩展名)");
scanf("%s",string);
f0=fopen(string,"w"); //新建一个文件
p1=head;
while(p1!=NULL)
{
fwrite(p1,sizeof(struct student),1,f0);
p1=p1->next;
}
printf("所有学生的信息已写入文件%s中\n",string);
fclose(f0);
}
void readfile0(struct student*head) //读取原始文件(所有学生的信息)
{
FILE *f0;
char string[40];
printf("请输入总信息的文件名:");
scanf("%s",string);
while((f0=fopen(string,"r"))==NULL)
{
printf("不能打开,重新输入\n");
scanf("%s",string);
}
while(!feof(f0))
{
fread(head,sizeof(struct student),1,f0);
}
printf("该文件已打开!");
fclose(f0);}
追问: 谢谢您,那个…不好意思能不能再帮我看看一个代码?
void divideCount(struct student* head,int i) //显示每个分数段各有多少人,其中i表示哪科成绩
{
struct student*head1;
head1=Bubble1(head,i);//冒泡排序法
outPrint(head1,i);
int c0,c1,c2,c3,c4;
int c5,c6,c7,c8,c9;
c0=c5=0;
struct student* p;
struct student* q;
p=head1;
q=head1;
下面的字数有要求贴不上来了。。能不能加您的QQ啊?
答案:struct student * readfile0( void ) //读取原始文件(所有学生的信息)
{
FILE *f0;
char string[40];
struct student *head=NULL , *tail , *data ;
printf("请输入总信息的文件名:"); scanf("%s",string);
while((f0=fopen(string,"r"))==NULL)
{
printf("不能打开,重新输入\n");
scanf("%s",string);
}
while(!feof(f0))
{
data=(struct student *)malloc( sizeof(struct student) );
if ( fread(data,sizeof(struct student),1,f0) <= 0 )
break ;
if ( NULL == head )
head=data;
else
tail->next=data;
tail=data;
}
tail->next=NULL ;
fclose(f0);
return head ;
}
void show_link( struct student * head )
{
struct student *data=head;
while(data!=NULL)
{
printf( "name=%s\n" , data->name );
printf( "name=%d\n" , data->易做图 );
printf( "name=%d\n" , data->score[0] );
printf( "name=%d\n" , data->score[1] );
printf( "name=%d\n" , data->score[2] );
printf( "name=%s\n" , data->number );
data=data->next;
}
}
int main()
{
struct student *head=NULL ;
head=readfile0() ;
show_link(head) ;
return 0;
}
上一个:vs2005 怎么添加头文件和cpp文件
下一个:怎样将CString变量赋值给字符数组?