简体   繁体   English

将.txt值推入向量

[英]Pushing .txt values into vector

I've been trying to figure out how to push things from a .txt into a vector and I haven't quite been successful, so I've strayed from my assignment and created a simple code to learn it. 我一直在尝试找出如何将.txt文件中的内容推送到向量中,但我还没有取得成功,因此我偏离了我的作业,创建了一个简单的代码来学习它。

I tried to find a similar post on here but I couldn't, so here's my code: 我试图在这里找到类似的帖子,但是找不到,所以这是我的代码:

This is what's in the text file: 这是文本文件中的内容:

32 34 5 6 243 2341 234 213 24 123 12354 124 432 12

This is the code: 这是代码:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main () {

vector <int> numbers;
int val;
int newval = 50;

  ifstream file ("text.txt");
   if ( file.is_open())
   {
       for ( int i = 0; i < newval ; i++)
       {
            numbers.push_back(val);
       }
   }else{
       cout << "unable to open file."<<endl;
   }

      for ( int i = 0; i < numbers.size(); i++){
        cout << numbers[i] << endl;
      }

 return 0;
}

What the code is doing is printing 50 zeros. 代码正在执行的操作是打印50个零。 I'm not sure what I'm doing wrong, and any insight would be much appreciated! 我不确定自己在做什么错,任何见解将不胜感激! thank you. 谢谢。

P. S - in the initial for loop I was going to do: PS-在最初的for循环中,我要做的是:

for ( int i = 0; i < numbers.size() ; i++)
   {
        numbers.push_back(val);
   }

but that didn't make any sense to me because the vector is initially empty. 但这对我来说没有任何意义,因为向量最初是空的。 If that's what I'm supposed to do, please explain. 如果那是我应该做的,请解释。 Thanks. 谢谢。

Unless I'm blind, you're never assigning a value to val . 除非我是盲人,否则您永远不会为val分配值。

If that is the case, you're getting 0s because that's the default value for int s. 如果真是这样,您会得到0,因为这是int的默认值。

To fix this, you'll need to actually assign a read value to val before adding it to numbers . 要解决此问题,您需要先实际为val分配一个读取值,然后再将其添加到numbers Also note that it will be read as a string most likely, so you'll have to convert it to an int before it can be assigned to val ; 还要注意,它很可能会以string形式读取,因此必须先将其转换为int然后才能将其分配给val but that's it's own set of problems. 但这就是它自己的问题。

So, After I receiving help from community members here ( carcigenicate and twalberg) I've resolved all problems with the code, and I'm posting it below to help anyone else with this kind of question. 因此,在我从这里的社区成员(carcigenicate和twalberg)获得帮助之后,我已经解决了代码的所有问题,并将其发布在下面,以帮助任何其他人解决此类问题。

This is the original text file: 这是原始文本文件:

32 34 5 6 243 2341 234 213 24 123 12354 124 432 12

Below is the code: 下面是代码:

#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

int main () {

vector <int> numbers;
int val=0;


  ifstream file ("text.txt");
   if ( file.is_open())
   {
     while  (! file.eof())
       {
       while (file >> val){
            numbers.push_back(val);
       }
     }
   }else{
       cout << "unable to open file."<<endl;
    }
      for ( int i = 0; i < numbers.size(); i++){
        cout << numbers[i] << endl;
      }

file.close();
 return 0;
}

What prints as output: 输出为什么:

32
34
5
6
243
2341
234
213
24
123
12354
124
432
12

Thank you community! 谢谢社区!

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

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