qq_33316199 2017-04-15 05:08 采纳率: 0%
浏览 1247

c语言:关闭程序,再读取文件时出现开头的乱码,求助

保存文件后的结果
读取文件后的显示

(请忽略功能文字,只是一个功能的完善,没有去修改)

以下是文件存储读取的代码

#include "record.h"

//文件存储操作函数
void save(const BOOK *top)
{
const BOOK *p;
FILE *fp;

if(count==0)
{

printf("没有记录可存!");
return;

}

if((fp=fopen("BOOK.TXT","wb"))==NULL)
{
printf("不能打开文件!\n");
exit(1);
}

printf("\n存文件\n");
p=top;

while(p)
{
fwrite(p,LEN,1,fp);
p=p->next;
}
fclose(fp);
printf("%d条记录已经存入文件,请继续操作。\n",count);
}
//文件读取操作函数
BOOK *load(BOOK *top)
{

FILE *fp;
BOOK *p,*old;
count=0;
top->next=NULL;
old=top;

if((fp=fopen("BOOK.TXT","rb"))==NULL)
{
printf("打不开文件!\n");
exit(1);
}
printf("\n取文件...\n");

while(!feof(fp))
{
ASK(p);
if(1!=fread(p,LEN,1,fp)) break;
else{
count=count+1;
p->next=NULL;
old->next=p;
old=p;
}
}
fclose(fp);

printf("取入%d条记录。",count);
return top;
}

/*
#include "record.h"
void writeToFile(){
FILE *out;
out = fopen( "BOOK.txt", "wb" );
if(out==NULL){
printf("无法打开文件!\n");
return;
}
p=head;
while(p!=NULL){
fwrite(p,LEN,1,out);
p=p->next;
printf("输出文件!\n");
}
fclose(out);
}

void readFromFile(){
FILE in;
in = fopen( "BOOK.txt", "rb" );
if(in==NULL){
printf("无法打开文件!\n");
return;
}
count=0;
head=NULL;
p=NULL;
old=NULL;
ASK(p);
while(!feof(in)){
if(fread(p,LEN,1,in)!=1)
break;
if(head==NULL){
head=p;
}else{
old->next=p;
}
old=p;
count++;
printf("读入文件!\n");
ASK(p);
}
old->next=NULL;
printf("readin count:%d\n",count);
fclose(in);
}*/

麻烦各位大神了

  • 写回答

1条回答 默认 最新

  • ljheee 2017-04-15 07:48
    关注

    第一次读取文件时,有正常关闭没

    评论

报告相同问题?

悬赏问题

  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)