结构数组写入txt和读取的有关问题
结构数组写入txt和读取的问题
求大神帮忙看看
能看看怎么改能保存2进制的文件,怎么改能保存和读取文本信息
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxSize 500
struct a{
int m;
char name[10];
int old,high,heavy;
};
int count=0;
void newspaper(struct a paper[]);
void searchpaper(struct a paper[],int no);
void save(struct a paper[]);
void recovery(struct a paper[]);
int main(void)
{
int choice,no;
struct a paper[MaxSize];
do{
printf("%d paper was Recorded!\nChoice:\n [1]:Newspaper\n[2]:searchpaper\n[3]:recovery\n",count);
scanf("%d",&choice);
switch(choice)
{
case 1: newspaper(paper);break;
case 2:printf("Input the paper num:");
scanf("%d",&no);
searchpaper(paper,no);
break;
case 3:recovery(paper);
break;
default:break;
}
}while(choice==1||choice==2||choice==3);
printf("Thanks for using!");
return 0;
}
void newspaper(struct a paper[])
{
struct a s;
if(count==MaxSize)
{
printf("The Pending is full!\n");
return ;
}
printf("Num:");
scanf("%d",&s.m);
printf("Name:");
scanf("%s",s.name);
printf("Old:");
scanf("%d",&s.old);
printf("High:");
scanf("%d",&s.high);
printf("Heavy:");
scanf("%d",&s.heavy);
paper[count]=s;
count++;
save(paper);
}
void searchpaper(struct a paper[],int no)
{
int i,flag=0;
if(count==0)
{
printf("NO paper in the pending!\n");
return ;
}
for(i=0;i<count;i++)
if(paper[i].m==no)
{
flag=1;
break;
}
if(flag)
{
printf("Num:\t");
printf("Name:\t");
printf("Old:\t");
printf("High:\t");
printf("Heavy:\t");
printf("\n");
printf("%d\t",paper[i].m);
printf("%s\t",paper[i].name);
printf("%d\t",paper[i].old);
printf("%d\t",paper[i].high);
printf("%d\t",paper[i].heavy);
printf("\n\n\n\n");
}
else
printf("404 Not Found!\n");
}
void save(struct a paper[])
{
FILE *fp;
int i;
if((fp=fopen("stu.txt","wb"))==NULL)
{
printf("can not open file!\n");
return;
}
for(i=0;i<count;i++)
fwrite(paper,sizeof(struct a),46,fp);
fclose(fp);
return 0;
}
void recovery(struct a paper[])
{
FILE *fp;
int i;
if((fp=fopen("stu.txt","rb"))==NULL)
{
printf("can not open file!\n");
return;
}
for(i=0;i<count;i++)
fread(paper,sizeof(struct a),46,fp);
fclose(fp);
return 0;
}
------解决思路----------------------
赵老师
------解决思路----------------------
能看看怎么改能保存2进制的文件,怎么改能保存和读取文本信息
首先是怎样改的问题:对于二进制文件最好是将他读出到内存中转换为方便可读的文本形式,然后再改掉这一段文本,将他重新使用二进制的方式保存到文本即可(文本原先的内容需要清空)。
求大神帮忙看看
能看看怎么改能保存2进制的文件,怎么改能保存和读取文本信息
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxSize 500
struct a{
int m;
char name[10];
int old,high,heavy;
};
int count=0;
void newspaper(struct a paper[]);
void searchpaper(struct a paper[],int no);
void save(struct a paper[]);
void recovery(struct a paper[]);
int main(void)
{
int choice,no;
struct a paper[MaxSize];
do{
printf("%d paper was Recorded!\nChoice:\n [1]:Newspaper\n[2]:searchpaper\n[3]:recovery\n",count);
scanf("%d",&choice);
switch(choice)
{
case 1: newspaper(paper);break;
case 2:printf("Input the paper num:");
scanf("%d",&no);
searchpaper(paper,no);
break;
case 3:recovery(paper);
break;
default:break;
}
}while(choice==1||choice==2||choice==3);
printf("Thanks for using!");
return 0;
}
void newspaper(struct a paper[])
{
struct a s;
if(count==MaxSize)
{
printf("The Pending is full!\n");
return ;
}
printf("Num:");
scanf("%d",&s.m);
printf("Name:");
scanf("%s",s.name);
printf("Old:");
scanf("%d",&s.old);
printf("High:");
scanf("%d",&s.high);
printf("Heavy:");
scanf("%d",&s.heavy);
paper[count]=s;
count++;
save(paper);
}
void searchpaper(struct a paper[],int no)
{
int i,flag=0;
if(count==0)
{
printf("NO paper in the pending!\n");
return ;
}
for(i=0;i<count;i++)
if(paper[i].m==no)
{
flag=1;
break;
}
if(flag)
{
printf("Num:\t");
printf("Name:\t");
printf("Old:\t");
printf("High:\t");
printf("Heavy:\t");
printf("\n");
printf("%d\t",paper[i].m);
printf("%s\t",paper[i].name);
printf("%d\t",paper[i].old);
printf("%d\t",paper[i].high);
printf("%d\t",paper[i].heavy);
printf("\n\n\n\n");
}
else
printf("404 Not Found!\n");
}
void save(struct a paper[])
{
FILE *fp;
int i;
if((fp=fopen("stu.txt","wb"))==NULL)
{
printf("can not open file!\n");
return;
}
for(i=0;i<count;i++)
fwrite(paper,sizeof(struct a),46,fp);
fclose(fp);
return 0;
}
void recovery(struct a paper[])
{
FILE *fp;
int i;
if((fp=fopen("stu.txt","rb"))==NULL)
{
printf("can not open file!\n");
return;
}
for(i=0;i<count;i++)
fread(paper,sizeof(struct a),46,fp);
fclose(fp);
return 0;
}
------解决思路----------------------
赵老师
------解决思路----------------------
能看看怎么改能保存2进制的文件,怎么改能保存和读取文本信息
首先是怎样改的问题:对于二进制文件最好是将他读出到内存中转换为方便可读的文本形式,然后再改掉这一段文本,将他重新使用二进制的方式保存到文本即可(文本原先的内容需要清空)。