程序代码:
#include <stdio.h> #include <string.h> #define MAX 50 int main(void) { char buffer_1[MAX]; /* 读取文件1 */ char buffer_2[MAX]; /* 读取文件2 */ int count1 = 0; /* 记录buffer字符个数 */ int count2 = 0; int row1 = 0; /* 行数 */ int row2 = 0; char mode[100]; FILE *pfile1 = NULL; /* 调用文件的指针 */ FILE *pfile2 = NULL; FILE *pfile3 = NULL; char *filename1 = "E:\\才、\\Test\\1.txt"; /* 文件路径 */ char *filename2 = "E:\\才、\\Test\\2.txt"; char *filename3 = "E:\\才、\\Test\\3.txt"; if(!(pfile1 = fopen(filename1, "r"))) { printf("读取文件%s失败\n",filename1); return 1; } if(!(pfile2 = fopen(filename2, "r"))) { printf("读取文件%s失败\n",filename2); return 1; } if(!(pfile3 = fopen(filename3, "w"))) { printf("读取文件%s失败\n"); return 1; } sprintf(mode, "相同字符串:\n%%-%ds\n%%-%ds\n文件1行号: %%-4d文件2行号:%%-4d\n\n", MAX, MAX); for(;;) { row1++; /* 递增行数 */ while((buffer_1[count1++] = fgetc(pfile1)) != '\n'); buffer_1[--count1] = '\0'; if(feof(pfile1)) break; for(;;) { row2++; count2 = 0; while((buffer_2[count2++] = fgetc(pfile2)) != '\n' && buffer_2[count2-1] != EOF ); if(feof(pfile2)) break; buffer_2[--count2] = '\0'; if(strcmp(buffer_1, buffer_2) == 0) fprintf(pfile3, mode, buffer_1, buffer_2, row1, row2); } rewind(pfile2); count1 = 0; row2 = 0; } fclose(pfile1); fclose(pfile2); fclose(pfile3); return 0; }
I have not failed completely