编写程序,从第1题的文件f1.dat中,将数据读出,存入另外一个文件f3.dat中。另外,需要在原本的第5个和第6个整数之间再插入一个整数(这个整数由屏幕输入)一并存入新文件中。
#include <stdio.h>
#include <stdlib.h>
int main()
{ FILE *fp1,*fp2;
int num[10],a,i,n;
if((fp1=fopen("D:\\codeblock\\shiyanyi1\\f1.dat","r"))==NULL)//用磁盘地址打开,(找到文件右击,再点属性,复制位置(注意程序中地址连接用\\))
{
printf("File open error\n");
exit(0);
}
if((fp2=fopen("f3.dat","w"))==NULL)
{
printf("File open error\n");
exit(0);
}
for(i=0;i<10;i++)
{
fscanf(fp1,"%d",&num[i]);//从f2文件中输入
if(i==5)//插入数字
{
scanf("%d",&a);
fprintf(fp2,"%d ",a);
}
fprintf(fp2,"%d ",num[i]);
}
if(fclose(fp1))
{
printf("Can not close the file\n");
exit(0);
}
if(fclose(fp2))
{
printf("Can not close the file\n");
exit(0);
}
return 0;
}