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;
}
标签:fp,fputc,return,fscanf,fgetc,res,fp1,fopen,buf
From: https://blog.csdn.net/qq_51852604/article/details/139508039