簡體   English   中英

解析csv文件c ++

[英]Parse csv file c++

我必須解析具有相同基本結構的幾個csv文件,我必須將值保存在不同的矩陣中。 我想將每個表保存在一個矩陣中,但問題是我對endline字符有一些問題。 我試圖使用getline函數,但是在解析表時我無法終止while循環。

我用這個代碼:

// 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

每個表由其他表分隔一個空行。 有人可以幫我看每張桌子嗎? 非常感謝

您可以使用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;
}

也許嘗試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()用於從最后一個分號到結尾的其余行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM