鱼C论坛

 找回密码
 立即注册
查看: 1491|回复: 0

二进制文件的读取报错

[复制链接]
发表于 2014-12-27 15:02:34 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
两个程序,第一个程序 录入成绩,用二进制写到文件中,读取出来没有问题。
第二个程序 读取上一个程序建立的二进制文件出错。查了很久都不知道哪里的问题.... 求解答..
  1. #include<stdio.h>
  2. #include<stdlib.h>

  3. void Input( struct List *p );
  4. struct List *Creat();
  5. int n = 0 ;

  6. struct List
  7. {
  8.         int Num;
  9.         int Score;
  10.         struct List *Next;
  11. }*head=NULL,*read=NULL;

  12. void main()
  13. {
  14.         FILE *fp_read, *fp_write;

  15.         read = head = Creat();
  16.         fp_write = fopen("list", "wb");
  17.         do
  18.         {
  19.                 if ( !fwrite(head,sizeof(struct List), 1, fp_write) )
  20.                 {
  21.                         printf("输入时打开文件错误");
  22.                         return;
  23.                 }
  24.                 head = head->Next;
  25.         }while( head );
  26.         printf("写入成绩成功!\n");
  27.         fclose(fp_write);
  28.         fp_read = fopen("list", "rb");
  29.         do
  30.         {
  31.                 if ( !fread(read,sizeof(struct List), 1, fp_read) )
  32.                 {
  33.                         printf("读写时打开文件错误");
  34.                         return;
  35.                 }
  36.                 printf("排名 %d\n", read->Num);
  37.                 printf("分数 %d\n", read->Score);
  38.                
  39.         }while( read->Next != NULL );
  40.         fclose(fp_read);
  41.         system("pause");
  42. }

  43. struct List *Creat()
  44. {
  45.         struct List *p1, *p2, *head = NULL;                //p1,p2用来创建链表, head用来标记链表起始位置
  46.         
  47.         p1 = p2 = malloc( sizeof(struct List) );
  48.         Input( p1 );
  49.         while ( p1->Num != 0 )                //如果上一次读入的数据有效则继续(输入0为录入结束的标志)
  50.         {
  51.                 n++;
  52.                 if ( n == 1 )
  53.                 {
  54.                         head = p1;                        //标记起始位置
  55.                         head->Next = NULL;
  56.                 }
  57.                 else
  58.                 {
  59.                         p2->Next = p1;               
  60.                 }
  61.                 p2 = p1;
  62.                 p1 = malloc(12);
  63.                 Input( p1 );
  64.         }
  65.         p2->Next = NULL;
  66.         printf("成绩列表创建成功!!");
  67.         return head;
  68. }

  69. void Input( struct List *p )
  70. {
  71.         printf("请输入排名:");
  72.         scanf("%d", &p->Num);
  73.         if ( p->Num == 0 )
  74.         {
  75.                 return;
  76.         }
  77.         printf("请输入分数:");
  78.         scanf("%d", &p->Score);
  79. }<div class="blockcode"><blockquote>#include<stdio.h>
  80. #include<stdlib.h>

  81. struct List
  82. {
  83.         int Num;
  84.         int Score;
  85.         struct List *Next;
  86. }*head=NULL;

  87. void main()
  88. {
  89.         FILE *f_read;

  90.         if ( !(f_read = fopen("list", "rb+")) )
  91.         {
  92.                 printf("打开文件时错误");
  93.                 return;
  94.         }
  95.         do
  96.         {
  97.                 if ( !fread(head,sizeof(struct List), 1, f_read) )
  98.                 {
  99.                         printf("读入时打开文件错误");
  100.                         break;
  101.                 }
  102.                 printf("排名 %d\n", head->Num);
  103.                 printf("分数 %d\n", head->Score);
  104.                
  105.         }while( head->Next != NULL );
  106.         fclose( f_read );
  107.         system("pause");
  108. }
复制代码

第一个程序运行正常

第一个程序运行正常

这里的问题,但不知道原因

这里的问题,但不知道原因
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-18 10:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表