马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
两个程序,第一个程序 录入成绩,用二进制写到文件中,读取出来没有问题。
第二个程序 读取上一个程序建立的二进制文件出错。查了很久都不知道哪里的问题.... 求解答..#include<stdio.h>
#include<stdlib.h>
void Input( struct List *p );
struct List *Creat();
int n = 0 ;
struct List
{
int Num;
int Score;
struct List *Next;
}*head=NULL,*read=NULL;
void main()
{
FILE *fp_read, *fp_write;
read = head = Creat();
fp_write = fopen("list", "wb");
do
{
if ( !fwrite(head,sizeof(struct List), 1, fp_write) )
{
printf("输入时打开文件错误");
return;
}
head = head->Next;
}while( head );
printf("写入成绩成功!\n");
fclose(fp_write);
fp_read = fopen("list", "rb");
do
{
if ( !fread(read,sizeof(struct List), 1, fp_read) )
{
printf("读写时打开文件错误");
return;
}
printf("排名 %d\n", read->Num);
printf("分数 %d\n", read->Score);
}while( read->Next != NULL );
fclose(fp_read);
system("pause");
}
struct List *Creat()
{
struct List *p1, *p2, *head = NULL; //p1,p2用来创建链表, head用来标记链表起始位置
p1 = p2 = malloc( sizeof(struct List) );
Input( p1 );
while ( p1->Num != 0 ) //如果上一次读入的数据有效则继续(输入0为录入结束的标志)
{
n++;
if ( n == 1 )
{
head = p1; //标记起始位置
head->Next = NULL;
}
else
{
p2->Next = p1;
}
p2 = p1;
p1 = malloc(12);
Input( p1 );
}
p2->Next = NULL;
printf("成绩列表创建成功!!");
return head;
}
void Input( struct List *p )
{
printf("请输入排名:");
scanf("%d", &p->Num);
if ( p->Num == 0 )
{
return;
}
printf("请输入分数:");
scanf("%d", &p->Score);
}<div class="blockcode"><blockquote>#include<stdio.h>
#include<stdlib.h>
struct List
{
int Num;
int Score;
struct List *Next;
}*head=NULL;
void main()
{
FILE *f_read;
if ( !(f_read = fopen("list", "rb+")) )
{
printf("打开文件时错误");
return;
}
do
{
if ( !fread(head,sizeof(struct List), 1, f_read) )
{
printf("读入时打开文件错误");
break;
}
printf("排名 %d\n", head->Num);
printf("分数 %d\n", head->Score);
}while( head->Next != NULL );
fclose( f_read );
system("pause");
}
|