1.fscanf/fprintf
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* sq = fopen("./01.c","r");
FILE* sq1 = fopen("./02.c","r+");
if(sq == NULL)
{
perror("fopen");
return -1;
}
if(sq1 == NULL)
{
perror("fopen");
return -1;
}
printf("文件打开成功\n");
char arr;
while(fscanf(sq,"%c",&arr) != EOF)
{
fprintf(sq1,"%c",arr);
}
fclose(sq1);
fclose(sq);
return 0;
}
运行结果
2. fgetc\fputc
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* sq = fopen("./11.txt","r");
FILE* sq1 = fopen("./12.txt","r+");
if(sq == NULL)
{
perror("fopen");
return -1;
}
if(sq1 == NULL)
{
perror("fopen");
return -1;
}
printf("文件打开成功\n");
char arr;
while((arr=fgetc(sq)) != EOF)
{
fputc(arr,sq1);
}
fclose(sq);
fclose(sq1);
return 0;
}
运行结果
3.fgets\fputs
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
FILE* sq = fopen("./21.txt","r");
FILE* sq1 = fopen("./22.txt","r+");
if(sq == NULL)
{
perror("fopen");
return -1;
}
if(sq1 == NULL)
{
perror("fopen");
return -1;
}
printf("文件打开成功\n");
char arr[4] = "";
while(fgets(arr,sizeof(arr),sq) != NULL)
{
fputs(arr,sq1);
bzero(arr,sizeof(arr));
}
fclose(sq);
fclose(sq1);
return 0;
}
运行结果