1.使用fgets获取文件的行号
#include <myhead.h>
int main(int argc, const char *argv[])
{FILE *fp = fopen("./1.txt","r");if(fp==NULL){perror("fp");return -1;}char buf[30];int count = 0;while(fgets(buf,sizeof(buf),fp)){count++;}printf("行号是:%d\n",count);fclose(fp);return 0;
}
2、使用fputs和fgets拷贝文件
#include <myhead.h>
int main(int argc, const char *argv[])
{FILE *fp1 = fopen("./1.txt","r");if(fp1==NULL){perror("fp1");return -1;}FILE *fp2 = fopen("./2.txt","w");if(fp2==NULL){perror("fp2");return -1;}char buf[20];while(fgets(buf,sizeof(buf),fp1)){fputs(buf,fp2);printf("%s\n",buf);}fclose(fp1);fclose(fp2);return 0;
}
3、当日思维导图