[英]How to store words from a text file into an array?
I'm trying to read a text file called "olaola.dict", some sort of a dictionary, that currently holds 10 words each with 5 letters and store the words into an array of strings.我正在尝试读取一个名为“olaola.dict”的文本文件,它是某种字典,目前包含 10 个单词,每个单词有 5 个字母,并将这些单词存储到一个字符串数组中。 I'm using a char** pointer that points to an array of pointers in which each pointer points to a word from the dictionary.我正在使用一个指向指针数组的 char** 指针,其中每个指针都指向字典中的一个词。
So far, I've developed this code.到目前为止,我已经开发了这段代码。 It is printing the last 7 words correctly, but not the first three.它正确打印了最后 7 个单词,但不正确打印前三个单词。 The code is the following:代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ROW 10 /* 10 words */
#define COL 6 /* 5 letters in each word + Null terminator*/
int main(void) {
int i, j;
FILE *fp = NULL;
char **ptr = NULL;
ptr = (char**)malloc(ROW * sizeof(char*));
fp = fopen("olaola.dict","r");
for (i=0 ; i < 15; i++) {
if ((ptr[i] = malloc(sizeof(char) * COL)) == NULL) {
printf("unable to allocate memory \n");
return -1;
}
fscanf(fp, "%s", ptr[i]);
}
for(i = 0; i < ROW; i++){
printf("Word %d:%s\n", i+1,ptr[i]);
}
for (i=0 ; i < ROW; i++)
free(ptr[i]);
free(ptr);
fclose(fp);
return 0;
}
/*
ptr[] 0 1 2 3 4 5 6
+--------+ +---+---+---+---+---+---+---+
| --|------->| w | o | r | d | 0 | 0 |\0 |
+--------+ +---+---+---+---+---+---+---+
| --|------->| w | o | r | d | 0 | 1 |\0 |
+--------+ +---+---+---+---+---+---+---+
| --|------->| w | o | r | d | 0 | 2 |\0 |
+--------+ +---+---+---+---+---+---+---+
| --|------->| w | o | r | d | 0 | 3 |\0 |
+--------+ +---+---+---+---+---+---+---+
| --|------->| w | o | r | d | 0 | 4 |\0 |
+--------+ +---+---+---+---+---+---+---+
*/
The output is: output 是:
Word 1:[p:�
Word 2:0[p:�
Word 3:P[p:�
Word 4:Carlo
Word 5:Andre
Word 6:MESSI
Word 7:Arroz
Word 8:Doces
Word 9:Carro
Word 10:Tevez
Here is also the text file olaola.dict这也是文本文件 olaola.dict
lista
Sabes ontem Carlo
Andre
MESSI
Arroz Doces Carro Tevez
As you can see, there is no specific position for the words to be.如您所见,没有具体的 position 字样。 What would be the correct way to solve this problem?解决这个问题的正确方法是什么? (In another note, would it be wiser and more efficient to use a 2D array, even though the dictionary can have like 5k words?) (在另一个注释中,使用二维数组会更明智、更有效吗,即使字典可能有 5k 个单词?)
you problem is in this line你的问题在这一行
for (i=0 ; i < 15; i++) {
it's supposed to be:它应该是:
for (i=0 ; i < ROW; i++)
because you should allocate till ptr[9]
, but you are allocating till ptr[14]
which will produce undefined behavior as the size of the array = 10因为你应该分配到ptr[9]
,但是你分配到ptr[14]
这将产生未定义的行为,因为数组的大小 = 10
I noticed that the first for loop goes from zero to 14 instead of from zero to 9. This means that the code writes beyond the boundaries of the array.我注意到第一个 for 循环是从 0 到 14,而不是从 0 到 9。这意味着代码写入超出了数组的边界。 I assume that this writes to the memory being allocated for the first 3 words and this is the reason why you see the wrong values for these words.我假设这写入分配给前 3 个单词的 memory,这就是您看到这些单词的错误值的原因。 Correct the for loop and see if this solves the issue.更正 for 循环并查看是否可以解决问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.