简体   繁体   English

C - 打印字符数组 *

[英]C - print array of char *

I am trying to read thousands of words and print only the first 15 but the only word that it prints is the very last one in the array where I stored the words.我正在尝试读取数千个单词并仅打印前 15 个单词,但它打印的唯一单词是我存储单词的数组中的最后一个单词。

g++ --version g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 g++ --version g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3

From the following answer I derived the code below to read a file line by line C read file line by line从下面的答案中,我导出了下面的代码来逐行读取文件 C逐行读取文件

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

unsigned char *words[25143];

int readDictionary(void)
{
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
        ssize_t read;
        int count = 0;


    fp = fopen("dictionary.txt", "r");
    if (fp == NULL)
        {
        printf("failed to open file");
        exit(EXIT_FAILURE);
        }
    while ((read = getline(&line, &len, fp)) != -1) {
                printf("%s", line);
                words[count] =(unsigned char*) line;
                count++;
    }

    fclose(fp);
    if (line)
        free(line);

}

int main (void)
{

  readDictionary();
  printf("we just read the dictionary\n");
  for (int k= 0; k <15; k++)
  {
        printf("%d      %s",k,(unsigned char*)words[k]);
  }
}

Take a look at description of getline , which tells that it'll only allocate buffer if line passed is NULL.看一下getline 的描述,它告诉它只有在传递的行为 NULL 时才分配缓冲区。 It'll only be NULL the first time and consequently will reuse the buffer or increase it if line doesn't fit.它只会在第一次为 NULL,因此将重用缓冲区或在行不适合时增加它。

If you want it to allocate separate buffers for every word, do如果您希望它为每个单词分配单独的缓冲区,请执行

printf("%s", line);
words[count] =(unsigned char*) line;
line = NULL;
count++;

and remove the并删除

if (line)
    free(line);

but don't forget to free all non-NULL entries of words somewhere later on.但不要忘记稍后在某处释放所有非空词条目。

All pointers in your array are getting set to the same char * (line) which is getting overwritten every iteration in the loop leaving you with an array of char pointers that has the same pointer in each index of the array and that pointer points to a location in memory that has been written over and over until you overwrite it with the last item in the dictionary.数组中的所有指针都被设置为相同的 char *(行),循环中的每次迭代都会被覆盖,留下一个 char 指针数组,该数组的每个索引中都有相同的指针,并且该指针指向一个内存中的位置已被一遍又一遍地写入,直到您用字典中的最后一项覆盖它为止。 To do this the way you are wanting to line would need to use a different char * for each iteration in your loop.要以您想要的方式执行此操作,您需要为循环中的每次迭代使用不同的 char *。

to print a whole array/vector you need to iterate through all the items.要打印整个数组/向量,您需要遍历所有项目。 example: you have a vector of 5 units, to print all of it you need to例如:你有一个 5 个单位的向量,要打印所有你需要的向量

//Pseudo code
for(int i = 0;i < vectorex;i++)
    {
     print vectorex[i];
    }

this way it will iterate throught all anwser's and print it all这样它就会遍历所有的 anwser 并打印出来

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM