2013-04-04 23 views
0

这是我的程序从文本文件中读取数据并将其存储到一个向量,即时通讯编辑有困难,任何建议将是伟大的。 编号喜欢在这个例子中调用年份和月份的所有数据。我希望它的东西简单地错过了。错误与读取数据返回一个向量

#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; 
    } 
} 
+1

Post编译器错误。还有'std :: vector','std :: string','std :: getline()'和'std :: cout'。 – hmjd 2013-04-04 10:45:27

+0

“我有麻烦”是一个模糊的问题描述。你具体做什么?当你这样做时,究竟发生了什么? – 2013-04-04 10:45:38

+0

还有一个错误:期待一个声明。它不会编译,我基本上想读取存储在向量中的一些选定的数据。返回 – jaylad 2013-04-04 10:47:38

回答

0

这里是一个固定的版本与解释,因为还没有任何真正的答案。

#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() 
{ 
    std::vector<Weather> data_weather; 
    std::string line; 
    std::ifstream myfile("weatherdata.txt"); 
    if (myfile.is_open()) 
    { 
     int count = 0; // you gotta put it in front of the while loop or you'll 
       // create a new variable and initialize it to 0 every time 
       // you enter the loop 
     while (getline(myfile, line)) 
     { 
      // int count = 0; // placed it just before the loop 
      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++; // you're still inside the if (count > 8) 
        // you'd probably want to have it outsided the if statement 
        // instead. 
       } 
      } 
      ++count; //the count from inside. it'll probably do what you wanted 
        //it to do here. 

      // you'll probably want to run the for loop AFTER the while loop 
      // when the data_weather vector got fully filled 
     } 
     // the for loop from inside the while now outside 
     for (auto it = data_weather.begin(); it != data_weather.end(); ++it) 
     { 
      std::cout << it->year << " " << it->month << std::endl; 
      myfile.close(); 
     } 
    } 
    else 
    { 
     std::cout << "unable to open file"; 
    } 
    scat::pause("\nPress <Enter> to end the program."); 
    return 0; 
} 
+0

非常感谢,那是固定的 – jaylad 2013-04-04 14:05:06

+0

即时通讯没有得到任何错误的代码,但是当我在控制台运行它,我所得到的是:“按结束该程序。”没有数据显示。 – jaylad 2013-04-05 07:28:52

+0

抱歉,所有即时获取现在是“无法打开文件”,不知道为什么? – jaylad 2013-04-05 10:26:04