这是我的程序从文本文件中读取数据并将其存储到一个向量,即时通讯编辑有困难,任何建议将是伟大的。 编号喜欢在这个例子中调用年份和月份的所有数据。我希望它的东西简单地错过了。错误与读取数据返回一个向量
#include <iostream>
#include <libscat.h>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
struct Weather
{
int year;
int month;
double tempmax;
double tempmin;
};
int main()
{
vector<Weather> data_weather;
string line;
std::ifstream myfile ("weatherdata.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{ int count = 0;
if (count > 8)
{
std::istringstream buffer(line);
int year, mm;
double tmax, tmin;
if (buffer >> year >> mm >> tmax >> tmin)
{
Weather objName = {year, mm, tmax, tmin};
data_weather.push_back(objName);
count++;
}
}
for (auto it = data_weather.begin(); it != data_weather.end(); it++){
std::cout << it->year << " " << it->month << std::endl;}
myfile.close();
}
else
{
cout << "unable to open file";
}
scat::pause("\nPress <ENTER> to end the program.");
return 0;
}
}
Post编译器错误。还有'std :: vector','std :: string','std :: getline()'和'std :: cout'。 – hmjd 2013-04-04 10:45:27
“我有麻烦”是一个模糊的问题描述。你具体做什么?当你这样做时,究竟发生了什么? – 2013-04-04 10:45:38
还有一个错误:期待一个声明。它不会编译,我基本上想读取存储在向量中的一些选定的数据。返回 – jaylad 2013-04-04 10:47:38