2013-04-04 62 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