gpt4 book ai didi

c - 用普通 C 将文本文件读入数组

转载 作者:行者123 更新时间:2023-12-02 08:50:43 25 4
gpt4 key购买 nike

有没有办法用纯 C 将文本文件读入一维数组?这是我尝试过的(我正在写刽子手):

int main() {
printf("Welcome to hangman!");

char buffer[81];
FILE *dictionary;
int random_num;
int i;
char word_array[80368];

srand ( time(NULL) );

random_num = rand() % 80368 + 1;
dictionary = fopen("dictionary.txt", "r");

while (fgets(buffer, 80, dictionary) != NULL){
printf(buffer); //just to make sure the code worked;
for (i = 1; i < 80368; i++) {
word_array[i] = *buffer;
}
}

printf("%s, \n", word_array[random_num]);
return 0;
}

这里有什么问题吗?

最佳答案

尝试改变一些东西;

首先;您正在存储一个字符。 word_array[i] = *buffer; 表示将单个字符(行中/缓冲区中的第一个字符)复制到 word_array 中的每个(和每个)单字符槽中。

其次,您的数组将包含 80K 个字符,而不是 80K 个单词。假设那是你的字典文件的长度,你不能用那个循环把它全部放在那里。

I'm assuming you have 80,368 words in your dictionary file. That's about 400,000 words less than /usr/share/dict/words on my workstation, though, but sounds like a reasonable size for hangman…

如果出于某种原因你有意想要一个一维数组,你必须做以下三件事之一:

  • 假设您在大型机上,每个单词使用 80 个字符:

      char word_array[80368 * 80];

    memcpy (&(word_array[80 * i]), buffer, 80);
  • 在一个巨大的缓冲区中创建一个并行数组,其中索引指向每一行的开头

       int last_char = 0;
    char* word_start[80368];
    char word_array[80368 * 80];
    for ( … i++ ) {
    memcpy (&word_array[last_char], buffer, strlen(buffer));
    word_start[i] = last_char;
    last_char += strlen(buffer);
    }
  • 切换到使用指向 char 的指针数组,每个槽一个字。

      char* word_array[80368];

    for (int i = 0; i < 80368, i++) {
    fgets (buffer, 80, dictionary);
    word_array[i] = strdup (buffer);
    }

我推荐后者,否则您必须猜测最大大小或在阅读时浪费大量 RAM。 (如果你的平均单词长度大约是 4-5 个字符,就像在英语中一样,你平均每个单词浪费 75 个字节。)

我还建议动态分配 word_array:

   int max_word = 80368;
char** word_array = malloc (max_word * sizeof (char*));

...如果您的字典大小发生变化,这可以让您更安全地阅读:

   int i = 0;
while (1) {
/* If we've exceeded the preset word list size, increase it. */
if ( i > max_word ) {
max_word *= 1.2; /* tunable arbitrary value */
word_array = realloc (word_array, max_word * sizeof(char*));
}
/* Try to read a line, and… */
char* e = fgets (buffer, 80, dictionary);
if (NULL == e) { /* end of file */
/* free any unused space */
word_array = realloc (word_array, i * sizeof(char*));
/* exit the otherwise-infinite loop */
break;
} else {
/* remove any \r and/or \n end-of-line chars */
for (char *s = &(buffer[0]); s < &(buffer[80]); ++s) {
if ('\r' == *s || '\n' == *s || '\0' == *s) {
*s = '\0'; break;
}
}
/* store a copy of the word, only, and increment the counter.
* Note that `strdup` will only copy up to the end-of-string \0,
* so you will only allocate enough memory for actual word
* lengths, terminal \0's, and the array of pointers itself. */
*(word_array + i++) = strdup (buffer);
}
}
/* when we reach here, word_array is guaranteed to be the right size */
random = rand () % max_word;
printf ("random word #%d: %s\n", random, *(word_array + random));

抱歉,这是匆忙发布的,所以我没有测试上面的内容。买者自负。

关于c - 用普通 C 将文本文件读入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8647792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com