#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i = 0, len1=0, len2=0;
int BUFSIZE = 1000;
char* string1[20];
char* string2[20];
FILE *fp1 = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if ((fp1 == 0)||(fp2 == 0)){
fprintf(stderr, "Error while opening");
return 0;
}
string1[i] = (char*)malloc(BUFSIZE);
string2[i] = (char*)malloc(BUFSIZE);
while (fgets(string1[i], BUFSIZE, fp1)) {
i++;
len1+=strlen(string[i]);
string1[i] = (char*)malloc(BUFSIZE);
len1+=strlen(string1[i]);
}
i = 0;
while (fgets(string2[i], BUFSIZE, fp2)) {
i++;
string2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
printf("\n");
printf("%d", len1);
int x;
for(x = 0; x<i; x++){
free(string1[x]);
free(string2[x]);
}
scanf("%d", x);
fclose(fp1);
fclose(fp2);
return 0;
}
感谢user1807597的帮助,我终于实现了读取两个字符串并存入数组。但是我仍然无法获取数组的长度,我尝试将 len+=strlen(string[i]); 在while循环中,但编译器在调试时中断。有人知道为什么吗?谢谢!