我正在尝试将数据保存到一个数组中,以便再次重复使用,数据采用以下格式并且来自 1964-2013,这只是一个片段,任何帮助都会很棒,干杯。
a b c d e f
1964 9 20.5 8.8 0 37.4
1964 10 13.6 4.2 5 77.8
1964 11 11.8 4.7 3 45.5
1964 12 7.7 0.1 17 65.1
1965 1 7.3 0.8 14 74.6
1965 2 6.5 0.1 13 3.3
到目前为止,这是我使用代码的地方:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
#include <vector>
#include <string>
struct Weather
{
int a;
int b;
double c;
double d;
int e;
double f;
double g;
};
int main()
{
std::vector<Weather> data_weather;
string line;
std::ifstream myfile ("weatherdata.txt");
if (myfile.is_open())
{
while ( getline(myfile, line) )
{
std::istringstream buffer(line);
int a_d, b_d, c_d, d_d, e_d, f_d, g_;
buffer >> a >> b >> c >> d >> e >> f >> g;
data_weather.push_back(Weather());
data_weather.back().a = a_d;
data_weather.back().b = b_d;
data_weather.back().c = c_d;
data_weather.back().d = d_d;
data_weather.back().e = e_d;
data_weather.back().f = f_d;
data_weather.back().g = g_d;
cout << line << endl;
}
myfile.close();
}
else
cout << "unable to open file";
scat::pause("\nPress <ENTER> to end the program.");
return 0;
}