简体   繁体   English

C - 在离开while循环后覆盖数组中的值

[英]C - values in array overriden after leaving while loop

I have some strings I add into an array in a while loop. 我在while循环中添加了一些字符串。 Everything is good and seems added inside the while loop, but when I leave the while loop and loop through the array to inspect the values, the last value added appears in all the indexes. 一切都很好,似乎在while循环中添加,但是当我离开while循环并循环遍历数组来检查值时,最后添加的值出现在所有索引中。

Here is the code: 这是代码:

    char buffer[50];
    char *keys[1000] = {""}; 
    int count = 0;

    FILE *file = fopen(argv[1], "r");
    if (file == NULL) {
            return 1;
    } 

    while (fgets(buffer, 100, file) != NULL) {
            buffer[strlen(buffer) - 1] = '\0';           
            printf("Buffer being read: %s\n", buffer);
            keys[count] = buffer;
            printf("Buffer after storing: %s, index: %d\n", keys[count], count);
            count++;
            if (count == 3) {
                    break;
            }
    }

    printf("Reading from stored array\n");
    printf("%s, index: %d\n", keys[0], 0);
    printf("%s, index: %d\n", keys[1], 1);
    printf("%s, index: %d\n", keys[2], 2);

And here is the output: 这是输出:

Buffer being read: 0DJz9J/xOBK/chDRPFvOwg==
Buffer after storing: 0DJz9J/xOBK/chDRPFvOwg==, index: 0
Buffer being read: IHKA7WHFdhQIRpqFmqT1ew==
Buffer after storing: IHKA7WHFdhQIRpqFmqT1ew==, index: 1
Buffer being read: TtD7k1Z+4PFLfl46xWOZgQ==
Buffer after storing: TtD7k1Z+4PFLfl46xWOZgQ==, index: 2
Reading from stored array
TtD7k1Z+4PFLfl46xWOZgQ==, index: 0
TtD7k1Z+4PFLfl46xWOZgQ==, index: 1
TtD7k1Z+4PFLfl46xWOZgQ==, index: 2

As you can see, after I leave the while loop, somehow the values are magically over ridden. 正如你所看到的,在我离开while循环之后,不知何故,这些值被神奇地覆盖了。

Any ideas what could be causing this? 可能导致这种情况的任何想法?

Thanks 谢谢

You have multiple problems in your code: 您的代码中存在多个问题:

buffer is defined with a size of 50 and you pass a maximum size of 100 to fgets : buffer定义为50,并将最大大小100传递给fgets

fgets(buffer, 100, file);

You should instead increase the buffer size and use sizeof to keep the use and definition in sync: 您应该改为增加缓冲区大小并使用sizeof来保持同步使用和定义:

fgets(buffer, sizeof buffer, file);

Furthermore, you should not assume that the buffer contains a final '\\n' after a successful fgets . 此外,在成功的fgets之后,您不应该假设缓冲区包含最后的'\\n' If the file does not end with a linefeed, the final read will not store one into the buffer. 如果文件没有以换行结束,则最终读取不会将一个存储到缓冲区中。 A simple and reliable way to remove the final '\\n' if present is this: 如果存在,删除最终'\\n'的简单可靠方法是:

buffer[strcspn(buffer, "\n")] = '\0';

You store the address of buffer in all your entries in the keys array: 您将buffer的地址存储在keys数组中的所有条目中:

keys[count] = buffer;

Be aware that this does not duplicate the contents of the array, but just stores its address. 请注意,这不会复制数组的内容,而只是存储其地址。 So all entries in keys point to the same buffer, that has the contents of the last line read (or even undefined contents if end of file was reached). 因此, keys所有条目都指向同一缓冲区,其中读取最后一行的内容(如果到达文件末尾,则甚至是未定义的内容)。 You can fix this by allocating a copy of the string: 您可以通过分配字符串的副本来解决此问题:

keys[count] = strdup(buffer);

But remember to free all these strings before returning for this function. 但请记住在返回此功能之前释放所有这些字符串。

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

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