【发布时间】:2021-04-09 22:41:47
【问题描述】:
我一直在尝试从包含两列浮点数据的文本文件中读取数据
1 2
3 4
4.5 6.5
2 4
现在,我尝试了stackoverflow中几个问题的方法,如this、this等。我写的最终代码是:
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
vector<float> a, b;
fstream file;
file.open("sample.txt", ios::in);
if(!file){
cout << "Error" <<endl;
return 0;
}
int number_of_lines = 0;
string line;
while(getline(file,line))
{
file >> a[number_of_lines];
file >> b[number_of_lines];
}
cout << number_of_lines << endl;
for (int i = 0; i < number_of_lines; i++) //code to print out the vectors
{
cout << a[i] << " " << b[i] <<endl;
}
return 0;
}
但它似乎不起作用。如何读取向量中的数据?
【问题讨论】:
标签: c++ vector text-files