简体   繁体   English

C 将文件中的单词存储到动态分配的数组中,存储不正确

[英]C Store words from a file into a Dynamic allocated array, not storing correctly

If I am dynamically allocating an array of pointers to chars(strings).如果我动态分配一个指向字符(字符串)的指针数组。 why I cant place the content of the *word inside a given position in my array like so array[i] = word or array[i] = *word .为什么我不能将 *word 的内容放在数组中给定的 position 中,就像这样array[i] = wordarray[i] = *word

I want to split and store the words in a file into a dynamically allocated array.我想将文件中的单词拆分并存储到动态分配的数组中。 Is this the right direction or is it done through a 2D array.这是正确的方向还是通过二维数组完成的。

int main(int argc, char *argv[]) {
   FILE *file = fopen(argv[1], "r");

   fseek(file, 0, SEEK_END);
   size_t size = ftell(file);
   fseek(file, 0, SEEK_SET);
   char *array = malloc(size * sizeof(char)+10);
   char *array2 = malloc(size * sizeof(char)+10);
   fread(array, 1, size, file);
   array[size] = '\0';
   char *word;
   word = strtok (array, " ,.-");
   int i = 0;
   while (i <= 9 && word != NULL ){
      for(i = 0; i < 10; i++) {
         array[i] = *word;
         strcpy(&array2[i], word);
         word = strtok (NULL, " ,.-");
      }
      fclose(file);
   }
   for (i = 0; i < 10; ++i)
      printf("array[%d] = %c\n", i, array2[i]);
   free(array);
   free(array2);
   fclose(file);
   return 0;
}

text to read.要阅读的文本。

This free test evaluates how well you understand what you

current output当前 output

array2[0] = T
array2[1] = f
array2[2] = t
array2[3] = e
array2[4] = h
array2[5] = w
array2[6] = y
array2[7] = u
array2[8] = w
array2[9] = y

What Id like to get.我想得到什么。

array2[0] = The
array2[1] = free
array2[2] = test
array2[3] = evaluates
array2[4] = how 
array2[5] = well
array2[6] = you
array2[7] = understand
array2[8] = what
array2[9] = you

You need two different arrays你需要两个不同的 arrays

The first is the buffer you read the data into - lets call that 'buffer', its a char*第一个是您将数据读入的缓冲区 - 让我们称之为“缓冲区”,它是一个 char*

The second is an array of pointers to 'strings' - lets call that 'words' its a char**第二个是指向“字符串”的指针数组 - 让我们称其为“单词”是一个字符**

int main(int argc, char* argv[]) {
    FILE* file = fopen("dict.txt", "r");

    fseek(file, 0, SEEK_END);
    size_t size = ftell(file);
    fseek(file, 0, SEEK_SET);
    char* buffer = malloc(size * sizeof(char) + 10);
    char** words = malloc(sizeof(char*) *  10);
    fread(buffer, 1, size, file);
    buffer[size] = '\0';
    char* word;
    word = strtok(buffer, " ,.-");
    int i = 0;
    while (i <= 9 && word != NULL) {
        for (i = 0; i < 10; i++) {
            words[i] = word;
            word = strtok(NULL, " ,.-");
        }
        fclose(file);
    }
    for (i = 0; i < 10; ++i)
        printf("array[%d] = %s\n", i, words[i]);
    free(buffer);
    free(words);
    fclose(file);
    return 0;
}

I will comment on the complete lack of tests to check if anything worked, PLus I have hardcede to 10 words - since you have done that in the loop it seems.我将评论完全缺乏测试来检查是否有任何工作,再加上我很难说到 10 个字——因为你似乎已经在循环中完成了。

Also note that the 'words' array if full of pointers to the 'buffer'.另请注意,如果“单词”数组中充满了指向“缓冲区”的指针。 If you delete or change 'buffer' 'words' will be invlaid如果您删除或更改“缓冲区”,“单词”将被禁用

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

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