简体   繁体   English

从文件中读取单词并将其存储在矢量C ++中

[英]Reading words from file and storing them in a vector c++

void print(vector<const char*> v) {
    for (vector<const char*>::const_iterator i = v.begin(); i != v.end(); ++i)
        cout << *i << ' ';
    cout << endl;
}
int main()
{
    vector<const char*> v;
    FILE *infile;
    infile = fopen("Text.txt", "r");
    char buffer[100];
    while ((fgets(buffer, 100, infile) != NULL)) {
        if (buffer[strlen(buffer) - 1] == '\n')
            buffer[strlen(buffer) - 1] = '\0';
        v.push_back((const char*)buffer);
        printf("%s\n", buffer);
    }
    fclose(infile);
    print(v);
return 0;

} }

So this is what I have so far. 这就是我到目前为止所拥有的。 At the printf (in the while loop), the words from the file are printed correctly. 在printf(在while循环中),文件中的单词被正确打印。 But the function print (which prints the elements of the vector) shows me only the last word repeated by the number of words from the file. 但是函数print(打印矢量的元素)仅显示最后一个单词,重复的次数是文件中单词的数量。 For example, let's say I have this in my file: 例如,假设我的文件中有此文件:

apple
banana
orange

The print function will print: 打印功能将打印:

orange
orange
orange

Edit: So I managed to get this to work. 编辑:所以我设法使它工作。 I made a list of "buffers" and it's working fine now. 我列出了“缓冲区”列表,现在工作正常。 Thanks everyone for your help! 谢谢大家的帮助!

The problem is that you are calling push_back on the same array buffer , each time storing the same pointer to its initial element in the vector. 问题是您在同一数组buffer上调用push_back ,每次在向量中存储指向其初始元素的相同指针。

This means, among other things, that no matter what you put into buffer , before or after calling push_back , the vector elements are going to point to it. 这意味着,无论您将什么放入buffer ,在调用push_back之前或之后, vector元素都将指向它。 In your case the result is that you see the last element that you have read, but if you wrote to buffer after reading the file, the vector would show that new value. 在您的情况下,结果是您看到了已读取的最后一个元素,但是如果在读取文件后写入buffer ,向量将显示该新值。

In order to solve this problem you need to push back copies of the buffer, instead of pushing the buffer itself. 为了解决此问题,您需要推回缓冲区的副本 ,而不是推入缓冲区本身。 The preferred approach in C++ would be switching away from vector<const char*> to vector<string> , because strings make the copying and resource management much easier for you. 在C ++中,首选方法是从vector<const char*>切换到vector<string> ,因为字符串使复制和资源管理对您而言更加容易。

If you must use pointers, make vector store non-const pointers; 如果必须使用指针,则使vector存储非const指针; otherwise you wouldn't be able to call delete on them. 否则,您将无法对其调用“ delete ”。 Instead of pushing back buffer allocate strlen(buffer)+1 char s with new[] , and strcpy the buffer into it before calling push_back . 不用推回buffer而是使用new[]分配strlen(buffer)+1 char ,然后在调用push_back之前将buffer strcpy放入其中。

Since an actual answer has already posted, your print() function could use some improvements: 由于已经发布了实际答案,因此您的print()函数可以使用一些改进:

void print(vector<const char*> const & v) {
    for (auto const chp : v) {
        cout << chp << ' ';
    }
    cout << '\n';
}

This version takes a reference instead of copying the whole vector. 此版本参考而不是复制整个向量。 And because the vector isn't changed by print() it's const . 而且因为向量不会被print()更改,所以它是const

Also if you want to iterate over all elements of a container you don't have to manage the iterators manually. 同样,如果要遍历容器的所有元素,则不必手动管理迭代器。

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

相关问题 读取输入文件并将它们存储在 C++ 中的 STL 向量中 - Reading input file and storing them in STL vector in C++ 从.txt 文件读取并将它们存储到向量中,其中 class 对象作为 c++ 中的值 - reading from .txt file and storing them to vector with class objects as values in c++ 从文本文件中读取整数并使用 C++ 将它们存储到向量中 - Reading integers from a text file and storing them into a vector using C++ 从文件中读取数字并将其存储在数组中的问题C ++ - Problem reading numbers from a file and storing them in an array c++ 从文件中读取多个字节并将它们存储在 C++ 中进行比较 - Reading multiple bytes from file and storing them for comparison in C++ 从文件中读取特定单词并将其存储在对象中 - Reading specific words from a file and storing them in an object 从文件中读取单词列表,逐个字符并将其存储在数组中 - Reading a list of words, char by char from a file and storing them in an array 从文件读取浮点数/单词/符号,仅将浮点数存储在数组C ++中 - Reading floats/words/symbols from a file and ONLY storing the floats in an array C++ 读取.csv文件并将值存储到vector和struct c ++中 - Reading in a .csv file and storing values into vector and struct c++ 从文件中读取并存储到数组 C++ - reading from file and storing into array C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM