[英]I read a string from a file and store it into an array, but how do I know the length of the array?
#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循環中,但編譯器在調試時中斷。 有人知道為什么嗎? 謝謝!
數組的長度有兩個主要選擇。
您將其做得足夠大,以至於您永遠不會使用更多的條目。
enum { MAX_LINES = 16 * 1024 }; char *lines[MAX_LINES]; size_t num_lines = 0;
您可以動態分配數組。
char **lines = 0; size_t max_lines = 0; size_t num_lines = 0; ... if (num_lines >= max_lines) { size_t new_lines = max_lines * 2 + 2; char **space = realloc(lines, new_lines * sizeof(*space)); if (space == 0) ...deal with out of memory... lines = space; max_lines = new_lines; }
兩種系統都可以工作。 動態分配在開始的前幾次會稍微有些麻煩,但是直到內存用盡時您才遇到問題,並且這些天來要花很長時間。 如果您猜錯了,固定分配可能會用完空間,並且如果僅處理小文件,則名義上會浪費內存。 這些天的“浪費”內存通常很小。
哪個更好取決於您希望程序處理的問題的范圍。 如果它們很小,那么固定但慷慨的分配將是明智且容易的。 如果它們很大,則動態分配會更明智。
我已經修改了代碼。我認為現在可以了。您在第一個while循環中計數有誤。並且i++
應該在len1+=strlen(string[i]);
在最后一個scanf語句中scanf("%d", x);
,您錯過了運算符'&'。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i = 0, len1 = 0, len2 = 0;
int BUFSIZE = 1000;
/*
you have too few lines here,
If my file contains more than 20 lines,tragedy will occur!
*/
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)!=NULL)
{
/*
i++;
*/
len1 += strlen(string1[i]);
i++;
string1[i] = (char*)malloc(BUFSIZE);
}
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);
*/
scanf("%d",&x);
fclose(fp1);
fclose(fp2);
return 0;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.