#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define STRLEN 255
int main()
{
FILE *fp1 = fopen("demo.txt", "r+");
if (fp1 != NULL)
{
FILE *fp2 = fopen("demo.bak", "a+");//文件存在,新建一个.bak文件
char str[STRLEN] = { 0 };
char ch = 0;
// 读取.txt中的文件到.bak中
while ((ch = getc(fp1)) != EOF)
{
putc(ch, fp2);
}
// 在.bak文件中输入。
while ((ch = getchar())!= '#')
{
putc(ch, fp2);
}
FILE *fp3 = fopen("demo.bak", "a+");
fclose(fp2);
int i = 0;
while ((str[i] = getc(fp3)) != EOF)
{
i++;
}
fputs(str, stdout);// 打印至屏幕
while ((ch = getchar()) != '#')
{
printf("请确认信息(# end)");
}
//将.bak中的文件读入到.txt中
fseek(fp1, 0, SEEK_SET);//设置游标在最前面,进行覆盖
fseek(fp3, 0, SEEK_SET);//设置游标在最前面,进行读取
while ((ch = getc(fp3)) != EOF)
{
putc(ch, fp1);
}
fclose(fp3);
}
fclose(fp1);
return 0;
}
文件的自动备份
最新推荐文章于 2023-05-27 15:55:51 发布