gpt4 book ai didi

c++ - 解析csv文件c++

转载 作者:行者123 更新时间:2023-11-30 02:34:03 28 4
gpt4 key购买 nike

我必须解析几个具有相同基本结构的 csv 文件,并且我必须将值保存在不同的矩阵中。我想将每个表保存在一个矩阵中,但问题是我对结束符有一些问题。我尝试使用 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

每个表格之间用一个空行分隔。有人可以帮我阅读每张 table 吗?非常感谢

最佳答案

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

关于c++ - 解析csv文件c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34903784/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com