[英]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.