c ++从csv文件输入到结构向量

时间:2013-12-02 23:51:45

标签: c++ csv vector struct

大家好我试图将所有逗号分隔的文本输入到结构向量中,但由于某种原因,来自cdv文件的double和int值不会被转移到结构中。你能告诉我我做错了吗?

提前谢谢

#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <string>
#include <cstdlib>


using namespace std;

struct dailyValues
{
    string name;
    string station_name;
    double elevation;
    double latitude;
    double longitude;
    int date;
    int maxPn;
    int tMax;
    int tMin;
    int observation;
};

int main()
{
    ifstream infile;
    infile.open("finalc++.csv");
    string temp;

    vector <dailyValues> allDays;

    dailyValues day;

    while (!infile.eof()) {
        getline(infile, day.name, ',');
        getline(infile, day.station_name, ',');
        getline(infile, temp, ',');
        day.elevation = stod(temp);
        getline(infile, temp, ',');
        day.latitude = stod(temp);
        getline(infile, temp, ',');
        day.longitude = stod(temp);
        getline(infile, temp, ',');
        day.date = stoi(temp);
        getline(infile, temp, ',');
        day.maxPn = stoi(temp);
        getline(infile, temp, ',');
        day.tMax = stoi(temp);
        getline(infile, temp, ',');
        day.tMin = stoi(temp);
        getline(infile, temp, ',');
        day.observation = stoi(temp);
        allDays.push_back(day);

    }
    for (int i = 0; i < 30; i++){
        cout << allDays[i].name << endl;
        cout << allDays[i].station_name << endl;

    }



    infile.close();

    return 0;
}

以下是我的cdv的样子,以防你想看。

STATION,STATION_NAME,ELEVATION,LATITUDE,LONGITUDE,DATE,MXPN,TMAX,TMIN,观察时间

GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130101,250,239,200,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130102,261,250,178,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130103,256,261,183,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130104,289,278,183,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130105,261,278,194,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130106,283,256,200,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130107,289,272,200,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130108,272,267,200,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130109,267,272,211,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130110,289,278,211,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130111,283,261,200,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130112,267,261,211,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130113,272,272,133,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130114,278,267,200,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130115,267,261,189,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130116,272,272,189,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130117,289,267,183,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130118,244,256,106,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130119,183,183,106,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130120,244,228,128,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130121,283,272,183,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130122,267,267,167,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130123,256,250,172,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130124,289,233,144,800 GHCND:USC00083909,HIALEAH FL US,3.7,25.8175,-80.2858,20130125,250,239,144,800

0 个答案:

没有答案