- 论坛徽章:
- 0
|
我修正了一些错误,但主要错误依然存在:- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #define SIZE 60
- #define MAX 20
- int main(void)
- {
- char names_1[SIZE][MAX];
- char names_2[SIZE][MAX];
- char * pnames_1[SIZE], * pnames_2[SIZE];
- char nms_1[SIZE][MAX];
- char nms_2[SIZE][MAX];
- char * pnms_1[SIZE], * pnms_2[SIZE];
- char * fnd[SIZE];
- int cnt_1 = 0, cnt_2 = 0, cnt_fnd = 0;
- int i,j;
- FILE *fp1, *fp2, *fp3;
- if((fp1 = fopen("list1.txt", "r")) == NULL)
- {
- fputs("Error reading \"listing1.txt\".", stderr);
- exit(1);
- }
- if((fp2 = fopen("list2.txt", "r")) == NULL)
- {
- fputs("Error reading \"listing2.txt\".", stderr);
- exit(1);
- }
- if((fp3 = fopen("results.txt", "w")) == NULL)
- {
- fputs("Error writing \"results.txt\".", stderr);
- exit(1);
- }
- while(cnt_1 <= SIZE && fgets(names_1[cnt_1], MAX, fp1) != NULL)
- {
- pnames_1[cnt_1] = names_1[cnt_1];
- pnms_1[cnt_1] = nms_1[cnt_1];
- cnt_1++;
- }
- while(cnt_2 <= SIZE && fgets(names_2[cnt_2], MAX, fp2) != NULL)
- {
- pnames_2[cnt_2] = names_2[cnt_2];
- pnms_2[cnt_2] = nms_2[cnt_2];
- cnt_2++;
- }
- for(i = 0; i < cnt_1; i++)
- {
- while(*(pnames_1[i]) && !isspace(*pnames_1[i]))
- {
- *pnms_1[i] = *pnames_1[i];
- pnames_1[i]++;
- pnms_1[i]++;
- }
- * pnms_1[i] = '\0';
- }
- for(i = 0; i < cnt_2; i++)
- {
- while(*(pnames_2[i]) && !isspace(*pnames_2[i]))
- {
- *pnms_2[i] = *pnames_2[i];
- pnames_2[i]++;
- pnms_2[i]++;
- }
- * pnms_2[i] = '\0';
- }
- for(i = 0; i < cnt_1; i++)
- for(j = 0; j < cnt_2; j++)
- if(strcmp(nms_1[i], nms_2[j]) == 0)
- {
- fnd[i] = nms_1[i];
- cnt_fnd++;
- }
- if(cnt_fnd > 0)
- {
- for(i = 0; i < cnt_fnd; i++)
- {
- fputs(fnd[i], fp3);
- putchar('\n');
- }
- fprintf(fp3, "\n%d found.", cnt_fnd);
- }
- else
- fputs("\nNo results found.", fp3);
- printf("\n%d\n", cnt_fnd);
- puts("\nDone!");
- fclose(fp1);
- fclose(fp2);
- fclose(fp3);
- return 0;
- }
复制代码 |
|