【发布时间】:2022-01-04 10:29:16
【问题描述】:
我正在尝试用 C++ 读取和解析我的 CSV 文件,但遇到了错误。
CSV 有 1-1000 行,总是 8 列。
通常我想做的是读取 csv 并仅输出符合过滤条件的行。例如,第 2 列是时间戳,并且仅在特定时间范围内。
我的问题是我的程序切断了一些行。
在数据在字符串记录变量中的点上,它没有被截断。一旦我将它推入 int/vector 的地图,它的截止值。我在这里做错了吗?
有人可以帮我确定问题的真正含义,或者甚至可以给我一个更好的方法吗?
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <map>
#include "csv.h"
using std::cout; using std::cerr;
using std::endl; using std::string;
using std::ifstream; using std::ostringstream;
using std::istringstream;
string readFileIntoString(const string& path) {
auto ss = ostringstream{};
ifstream input_file(path);
if (!input_file.is_open()) {
cerr << "Could not open the file - '"
<< path << "'" << endl;
exit(EXIT_FAILURE);
}
ss << input_file.rdbuf();
return ss.str();
}
int main()
{
int filterID = 3;
int filterIDIndex = filterID;
string filter = "System";
/*Filter ID's:
0 Record ID
1 TimeStamp
2 UTC
3 UserID
4 ObjectID
5 Description
6 Comment
7 Checksum
*/
string filename("C:/Storage Card SD/Audit.csv");
string file_contents;
std::map<int, std::vector<string>> csv_contents;
char delimiter = ',';
file_contents = readFileIntoString(filename);
istringstream sstream(file_contents);
std::vector<string> items;
string record;
int counter = 0;
while (std::getline(sstream, record)) {
istringstream line(record);
while (std::getline(line, record, delimiter)) {
items.push_back(record);
cout << record << endl;
}
csv_contents[counter] = items;
//cout << csv_contents[counter][0] << endl;
items.clear();
counter += 1;
}
【问题讨论】:
-
你有一个没有被切断的线的例子吗?
-
这是csv_contents的内容
0;"03.01.2022 09:28:30";"-1:00";"System";"Anwendung";"Runtime-Start von WinCC Runtime Advanced V17.0 am Bediengerõt lschiermann. Projekt: 'AuditTrail-Filter_V17.HMI - 0' Build 1 1;"03.01.2022 09:28:30";"-1:00";"System";"Anwendung";"Wechsel in die Betriebsart 'Online'.";;suavyY 2;"03.01.2022 09:28:31";"-1:00";"System";"Benutzerverwaltung";"Benutzerverwaltung importieren erfolgreich beendet.";;cDRs+P以0开头的行被截断以1开头的那一行不是 -
StackOverflow 有一个很棒的功能。您可以编辑您的问题。要获得此功能,只需点击
edit。没有必要在 cmets 中扩展您的问题。对自己的问题发表评论的唯一原因可以是对任何其他评论的回答。 -
欣赏小费! :)
-
代码说分隔符是,但数据说分隔符是;。这只是一个错字吗?