2017-03-26 36 views
0

hello我正在讀取CSV文件中的數據,並已成功解析出數據中的所有字符,現在我需要將每列中的字符串數據轉換爲適當的類型(int,double等)。我有我想要的以下類型聲明,然後從那裏每個變量應該更新爲儘可能多的行,所以我可以採取該變量並將其稱爲程序中的其他地方。請參閱下面解析向量中字符串的數據爲適當的數據類型C++

vector<std::vector<std::string> > matrix; 
int a; 
int b; 
int c; 
double c; 
double d; 
double e; 
double f; 
int f; 
double f; 
double f; 
string line; 
ifstream file("mynumbers - Copy.csv"); 

if (!file.is_open()) 
    perror("error while opening file"); 

    getline(file, line, '\n');//get the first line of columns names out 

while (getline(file, line,'\n')) //find the endline charcater 
    { 

     vector<string> vec; 
     string linevec; 
     istringstream ss(line);//allows for parsing each line 

     while (getline(ss, linevec, ',')) 
     { 

      vec.push_back(linevec);//push the parsed data for that line into a vector 
     } 

matrix.emplace_back(vec);//push each parsed line into another vector 
     } 
+0

您似乎缺少提問? –

+0

我試圖將數據完全解析後將其轉換爲我在上面聲明的佔用數據類型? – looneytunes24

+0

你有什麼問題呢?通常在使用CSV文件時,必須在解析文件之前決定列類型。 –

回答

0

您可能想檢查您的代碼的常見錯誤。像這樣的東西永遠不會編譯:

int c; 
double c; 

您還沒有main()功能

由於這些錯誤,這可能不適合你的需求完全吻合。假設每行有三個int S,4個double S,一個int,和那麼雙double S,我將宣佈一個「linedata」類像這樣:

#include <iostream> // for std::string 
#include <string> // for std::string 
#include <sstream> // for std::istringstream 
#include <utility> // for std::forward 
#include <fstream> // for std::ifstream 
#include <vector> // for std::vector 
#include <stdexcept> // for std::runtime_error 

class linedata 
{ 
    int a; 
    int b; 
    int c; 
    double d; 
    double e; 
    double f; 
    double g; 
    int h; 
    double i; 
    double j; 

public: 

    // constructs a linedata object 
    linedata(std::string&& line) 
    { 
     std::istringstream ss(std::forward<std::string>(line)); 
     char comma; 
     ss >> a >> std::ws >> comma >> std::ws >> // std::ws discarding extra spaces 
       b >> std::ws >> comma >> std::ws >> 
       c >> std::ws >> comma >> std::ws >> 
       d >> std::ws >> comma >> std::ws >> 
       e >> std::ws >> comma >> std::ws >> 
       f >> std::ws >> comma >> std::ws >> 
       g >> std::ws >> comma >> std::ws >> 
       h >> std::ws >> comma >> std::ws >> 
       i >> std::ws >> comma >> std::ws >> 
       j; 

     // check for errors. throw if it fails to parse 
     if(ss.fail()) throw std::runtime_error("Could not parse line!"); 

     std::cout << a << '\n' << b << '\n' << c << '\n' << d << '\n' << 
        e << '\n' << f << '\n' << g << '\n' << h << '\n' << 
        i << '\n' << j << std::endl; 
    } 
}; 

然後,在main(),您可以嘗試打開文件解析字符串:

int main() 
{ 
    std::vector<linedata> lines; 

    std::ifstream file("mynumbers - Copy.csv"); 
    if(! file.is_open()) 
     // personally I would not use perror because it is part of c and 
     // requires yet another header. I'd use this: 
     std::cerr << "Error occurred: " << std::strerror(errno) << std::endl; 

    std::string myline; 
    std::getline(file,myline); // the '\n' is not necessary. It gets a line. 
           // it already knows to look for '\n'. 

    while(std::getline(file,myline)) 
    { 
     lines.emplace_back(std::move(myline)); 
    } 
} 
+1

您的'逗號'輸入可以很容易地使用空格而不是逗號。使用'ss >> a >> std :: ws >>逗號>> b >> std :: ws >>逗號>> ...'會是一個額外的保障。你可能想要設置一個錯誤狀態或者拋出'linedata'構造沒有成功解析字符串。 – paddy

+0

好點。我現在會更新我的答案。 –

+0

有沒有必要使用std :::當你使用命名空間std這使得代碼看起來很醜 – looneytunes24