2016-01-20 162 views
1

我要解析具有相同的基本結構,多個CSV文件,我必須保存在不同的矩陣的值。 我想將每個表格保存在一個矩陣中,但問題是我在結束字符時遇到了一些問題。 我試圖使用getline函數,但在解析表時我無法終止while循環。解析CSV文件C++

我用這個代碼:

// MMDS.cpp : definisce il punto di ingresso dell'applicazione console. 
// 

#include "stdafx.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <map> 

using namespace std; 

int i = 0, j = 0 , z=0; 

int main() 
{ 
    ifstream file("I_30_2_02_02_1.csv"); // declare file stream 
    string value; 
    string intvalue; 
    string check; 
    int jobs; 
    int machines; 
    int resources; 
    vector<string> JobTime; 
    vector<string> MachineId; 
    vector<string> ProcTime; //archiviato come JobId MachineId ProcTime 

    //prime tre righe 
    getline(file, value, ';'); // #jobs 
    getline(file, intvalue, '\n'); 
    jobs = stoi(intvalue); 
    cout << "Jobs: " <<jobs << "\n"; 

    getline(file, value, ';'); //#machines 
    getline(file, intvalue, '\n'); 
    machines = stoi(intvalue); 
    cout << "Machines: " << machines << "\n"; 

    getline(file, value, ';'); //#resources 
    getline(file, intvalue, '\n'); 
    resources = stoi(intvalue); 
    cout << "Resources: " << resources << "\n"; 

    //scritte inutili 
    getline(file, value, ';'); 
    cout << value << " "; 
    getline(file, value, ';'); 
    cout << value << " "; 
    getline(file, value, '\n'); 
    cout << value << "\n"; 

    //primo ciclo 
    while (getline(file, intvalue)) { 

     getline(file, intvalue, ';'); 
     JobTime.push_back(intvalue); 
     getline(file, intvalue, ';'); 
     MachineId.push_back(intvalue); 
     getline(file, intvalue, '\n'); 
     ProcTime.push_back(intvalue); 
     //getline(file, intvalue, '\n'); 
    } 

    for (i = 0; i <= ProcTime.size(); i++) 
     cout << JobTime[i] << " " << MachineId[i] << " " << ProcTime[i] <<endl; 

    cin >> intvalue; 

    return 0; 
} 

CSV文件是:

#Jobs;30 
#Machines;2 
#Resources;4 

JobId;MachineId;PrTime 
1;1;12 
2;0;97 
3;1;54 
4;1;83 
5;1;56 
6;0;5 
7;0;18 
8;1;17 
9;0;15 
10;0;83 

JobId;DueDate;RelDate;TardPenalty 
1;575;4;1 
2;563;70;2 
3;483;1;8 
4;519;68;1 
5;540;64;10 
6;546;126;8 
7;550;2;6 

每個表由其餘的跟一個空行分隔。 有人可以幫我閱讀每張表嗎? 非常感謝

+0

可能重複[如何在C++中讀取和解析CSV文件?](http://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files -in-c) –

+0

@SimonKraemer發佈了一個很好的答案。你沒有指定你在使用結束字符時遇到了什麼問題,但是[這個答案](http://stackoverflow.com/a/15328493/2744444)可能會有所幫助。或者至少讓你去。 – bratak

+0

我的問題是,當我讀了第一張表,我的程序繼續讀取第二張表,而我只想讀第一張。 – user3836982

回答

1

也許嘗試if (entry.empty()),或者類似的一些條件。 此外,我認爲getline()返回行的長度(0爲空,所以空行將> 0)。所以它應該像找到空白行的大小一樣簡單。

while (getline(file, intvalue)) 
{ 
    if (intvalue > 0) 
    { 
     getline(file, intvalue, ';'); 
     JobTime.push_back(intvalue); 

     getline(file, intvalue, ';'); 
     MachineId.push_back(intvalue); 

     getline(file, intvalue, '\n'); 
     ProcTime.push_back(intvalue); 

    } else { 
     break; 
    } 
} 

或類似的東西。如果intvalue > 0不起作用,那麼找到空行的大小並將其用作條件。

編輯: 作爲替代方案,getline()也可以返回一個字符串。在我看來,這有利於搜索。我在下面寫了一個快速的例子。

size_t pos; 
std::string str; 
std::string token; 
std::vector<std::string> line; 

// get the entire line 
while (getline(file, str)) 
{ 
    while ((pos = str.find(';')) != std::string::npos) 
    { 
     // get content up to next semicolon  
     token = str.substr(0, pos); 
     line.push_back(token); 
     str.erase(0, pos + 1); 
    } 
    // get content to the end 
    token = str.substr(0, pos); 
    line.push_back(token); 
} 

第二個while循環查找每個分號,然後將內容按下並擦除。在while循環之後,push_back()用於從最後一個分號到結尾的其餘行。

2

您可以使用peek()函數。
檢查file.peek()!= '\ n'
下面的代碼應該爲你工作。

#include <iostream> 
#include <fstream> 
#include <string> 
#include <vector> 
#include <map> 

using namespace std; 

int i = 0, j = 0 , z=0; 

int main() 
{ 
    ifstream file("I_30_2_02_02_1.csv"); // declare file stream 
    if(!file) 
     return 0; 
    string value; 
    string intvalue; 
    string check; 
    int jobs; 
    int machines; 
    int resources; 
    vector<string> JobTime; 
    vector<string> MachineId; 
    vector<string> ProcTime; //archiviato come JobId MachineId ProcTime 

    //prime tre righe 

    getline(file, value, ';'); // #jobs 
    getline(file, intvalue, '\n'); 
    jobs = stoi(intvalue); 
    cout << "Jobs: " <<jobs << "\n";  

    getline(file, value, ';'); //#machines 
    getline(file, intvalue, '\n'); 
    machines = stoi(intvalue); 
    cout << "Machines: " << machines << "\n"; 

    getline(file, value, ';'); //#resources 
    getline(file, intvalue, '\n'); 
    resources = stoi(intvalue); 
    cout << "Resources: " << resources << "\n"; 

    //scritte inutili 
    getline(file, value, ';'); 
    cout << value << " "; 
    getline(file, value, ';'); 
    cout << value << " "; 
    getline(file, value, '\n'); 
    cout << value << "\n"; 
    //primo ciclo 
    while (file.peek()!='\n') 
    { 

     getline(file, intvalue, ';'); 
     JobTime.push_back(intvalue); 
     getline(file, intvalue, ';'); 
     MachineId.push_back(intvalue); 
     getline(file, intvalue, '\n'); 
     ProcTime.push_back(intvalue); 
     //getline(file, intvalue, '\n'); 
    } 

    for (i = 0; i < ProcTime.size(); i++) 
     cout << JobTime[i] << " " << MachineId[i] << " " << ProcTime[i] <<endl; 

    cin >> intvalue; 

return 0; 
} 
+0

非常感謝!它適用於我的問題! – user3836982