
C语言 将一个磁盘文件中的信息复制到另一个磁盘文件中,要求使用 fread fwrite这两个函数来实现
我自己也有写一个,不过感觉不太对(代码如下),指点下我的程序或者重新写一个都行哈,谢谢啦~#include<stdio.h>#defineSIZE4structstude...
我自己也有写一个,不过感觉不太对(代码如下),指点下我的程序或者重新写一个都行哈,谢谢啦~
#include<stdio.h>
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu_1","w"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if( fwrite( &stud[i],sizeof(struct student_type),1,fp )!=1 )
printf("file write error\n");
fclose(fp);
}
void load()
{
FILE *fp;
int i;
if((fp=fopen("stu_1","r"))==NULL)
{
printf("cannot open infile\n");
return;
}
for(i=0;i<SIZE;i++)
if(fread(&stu_1,sizeof(struct student_type),1,fp)!=1)//读取stu_1文件中的信息,直到读不出信息(读到文件尾了),跳出循环。循环体中就是打印读到的信息和令存文件
printf("%-10s %4d %4d %-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
fclose(fp);
}
void save1()
{
FILE *fp;
int i;
if((fp=fopen("stu_2","w"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if( fwrite( &stud[i],sizeof(struct student_type),1,fp )!=1 )
printf("file write error\n");
fclose(fp);
}
int main()
{
int i;
for(i=0;i<SIZE;i++)
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
save();
load();
save1();
return 0;
}
其中的函数save是新建一个文件,load读这个文件,save1把这个文件的内容复制到另外一个文件 展开
#include<stdio.h>
#define SIZE 4
struct student_type
{
char name[10];
int num;
int age;
char addr[15];
}stud[SIZE];
void save()
{
FILE *fp;
int i;
if((fp=fopen("stu_1","w"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if( fwrite( &stud[i],sizeof(struct student_type),1,fp )!=1 )
printf("file write error\n");
fclose(fp);
}
void load()
{
FILE *fp;
int i;
if((fp=fopen("stu_1","r"))==NULL)
{
printf("cannot open infile\n");
return;
}
for(i=0;i<SIZE;i++)
if(fread(&stu_1,sizeof(struct student_type),1,fp)!=1)//读取stu_1文件中的信息,直到读不出信息(读到文件尾了),跳出循环。循环体中就是打印读到的信息和令存文件
printf("%-10s %4d %4d %-15s\n",stud[i].name,stud[i].num,stud[i].age,stud[i].addr);
fclose(fp);
}
void save1()
{
FILE *fp;
int i;
if((fp=fopen("stu_2","w"))==NULL)
{
printf("cannot open file\n");
return;
}
for(i=0;i<SIZE;i++)
if( fwrite( &stud[i],sizeof(struct student_type),1,fp )!=1 )
printf("file write error\n");
fclose(fp);
}
int main()
{
int i;
for(i=0;i<SIZE;i++)
scanf("%s%d%d%s",stud[i].name,&stud[i].num,&stud[i].age,&stud[i].addr);
save();
load();
save1();
return 0;
}
其中的函数save是新建一个文件,load读这个文件,save1把这个文件的内容复制到另外一个文件 展开
3个回答
展开全部
save是新建文件,应该不需要去write把,它创建一个文件就OK了,
其实save1就可以把创建和写内容一起作了,这样就可以少一次对创建文件的开关。
其实save1就可以把创建和写内容一起作了,这样就可以少一次对创建文件的开关。
展开全部
你的程序对的啊,就是这里大意了
if(fread(&stu_1,sizeof(struct student_type),1,fp)!=1)//读取stu_1文件中的信息,直到读不出信息(读到文件尾了),跳出循环。循环体中就是打印读到的信息和令存文件
/////////if(fread(&stud[i],sizeof(stru
if(fread(&stu_1,sizeof(struct student_type),1,fp)!=1)//读取stu_1文件中的信息,直到读不出信息(读到文件尾了),跳出循环。循环体中就是打印读到的信息和令存文件
/////////if(fread(&stud[i],sizeof(stru
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果你将整个结构体写入,再整个结构体读出,可能会有字节对齐所产生的乱码问题。可以将结构体的每个数据,一个一个的写入,然后一个一个的读出。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询