主函数 #include <stdio.h> int main(int argc, const char *argv[]) { FILE *fp1 = fopen("./1.c","r"); FILE *fp2 = fopen("./2.c","a+"); if (fp1 == NULL || fp2 == NULL) { perror("fopen"); return -1; } char str[50]; while (1) { if (fgets(str,sizeof(str),fp1) == NULL) { perror("fgets"); break; } fputs(str,fp2); } fseek(fp2,0,SEEK_SET); while (1) { if ( fgets(str,sizeof(str),fp2) == NULL) { perror("fgets"); break; } printf("%s",str); } fclose(fp1); fclose(fp2); return 0; } 测试 root@VM-12-9-ubuntu:7-27# cat 1.c hello world hello china root@VM-12-9-ubuntu:7-27# a fgets: Success hello world hello china fgets: Success root@VM-12-9-ubuntu:7-27# cat 2.c hello world hello china