1、使用这fscanf和fprintf两个函数实现文件的拷贝。
#include<myhead.h>
int main(int argc, const char *argv[])
{FILE *fp;if((fp=fopen("./1.txt","w"))==NULL){perror("fopen");return -1;}int num=100;printf("num=%d\n",num);fprintf(fp,"%d\n",num);fclose(fp);if((fp=fopen("./1.txt","r"))==NULL){perror("fopen");return -1;}int key=0;fscanf(fp,"%d",&key);printf("key=%d\n",key);fclose(fp);return 0;
}
代码演示
2、作业:fgets和fputs拷贝文件。
#include<myhead.h>
int main(int argc, const char *argv[])
{if(argc!=3){printf("外部传参错误\n");return -1;}FILE *fp1;fp1=fopen(argv[1],"w");//只写方式打开文件 if(fp1==NULL){perror("fopen fp1");return -1;}FILE *fp2;fp2=fopen(argv[2],"r");if(fp2==NULL){perror("fopen fp2");return -1;}while(1){char ch=fgetc(fp2);//读取文件2.txt内容if(ch==EOF)//返回EOF读取结果{break;}fputc(ch,fp1);//写入文件1.txt}fclose(fp1);fclose(fp2);return 0;
}
代码演示
3、登录注册实现一遍。