1.fscanf\fprintf
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./1.c", "r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
FILE* fp1 = fopen("./2.c", "w+");
if(NULL == fp1)
{
perror("fopen1");
return -1;
}
int res;
char buf;
while(1)
{
res = fscanf(fp, "%c", &buf);
if(EOF == res)
break;
fprintf(fp1,"%c", buf);
}
fclose(fp);
fclose(fp1);
return 0;
}
2.fgetc\fputc
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./1.c", "r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
FILE* fp1 = fopen("./2.c", "w+");
if(NULL == fp1)
{
perror("fopen1");
return -1;
}
int res;
char buf;
while(1)
{
res = fgetc(fp);
if(EOF == res)
break;
fputc(res,fp1);
}
/* while(1)
{
res = fscanf(fp, "%c", &buf);
if(EOF == res)
break;
fprintf(fp1,"%c", buf);
}
*/
fclose(fp);
fclose(fp1);
return 0;
}
3.fgets\fputs
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./1.c", "r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
FILE* fp1 = fopen("./2.c", "w+");
if(NULL == fp1)
{
perror("fopen1");
return -1;
}
int* res=NULL;
char buf[100];
while(1)
{
res = fgets(buf, sizeof(buf), fp);
if(NULL == res)
break;
fputs(buf, fp1);
}
/* while(1)
{
res = fgetc(fp);
if(EOF == res)
break;
fputc(res,fp1);
}
while(1)
{
res = fscanf(fp, "%c", &buf);
if(EOF == res)
break;
fprintf(fp1,"%c", buf);
}
*/
fclose(fp);
fclose(fp1);
return 0;
}
4.文件对比
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE* fp = fopen("./1.c", "r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
FILE* fp1 = fopen("./2.c", "r");
if(NULL == fp1)
{
perror("fopen1");
return -1;
}
int res,res1;
while(res = fgetc(fp)!=EOF && (res1 = fgetc(fp1))!=EOF)
{
if(res != res1)
{
printf("不一样\n");
fclose(fp);
fclose(fp1);
}
}
if(res==EOF && res1 == EOF)
{
printf("一样\n");
}else{
printf("不一样长,也不一样\n");
}
fclose(fp);
fclose(fp1);
return 0;
}