#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
int main()
{
char input[20], output[20];
printf("输入要读入的文件名:");
scanf("%s", input);
printf("输入要输出的文件名:");
scanf("%s", output);
//打开文件
FILE* in, * out, * ch;
in = fopen(input, "r"); //只读 打开输入文件
if (in == NULL)
{
perror("fopen");
exit(0);
}
out = fopen(output, "w"); //只写 打开输出文件
if (out == NULL)
{
perror("fopen");
exit(0);
}
//操作文件
//读入一个字符后,读写位置自动后移一位!
while ( (ch = fgetc(in)) != EOF ) //文件尾标识符EOF,值-1
// while( !feof(in) ) //feof()函数:检测文件尾标识符是否被读过。已读为1,未读为0
fputc(ch, out);
//文件读写出错检测
if ( feof(in) )
printf("文件尾标识符EOF已读过,文件正常读入...");
else if ( ferror(in) ) //ferror()函数:检测文件是否遇到错误。出错非0,未出错为0
printf("文件因读取出错结束");
//关闭文件
fclose(in);
fclose(out);
in = out = NULL;
return 0;
}
将一个磁盘文件信息复制给另一个磁盘文件
于 2023-02-25 19:35:04 首次发布