【问题标题】:Why does getline() cut off CSV Input?为什么 getline() 会切断 CSV 输入?
【发布时间】: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 中扩展您的问题。对自己的问题发表评论的唯一原因可以是对任何其他评论的回答。
  • 欣赏小费! :)
  • 代码说分隔符是,但数据说分隔符是;。这只是一个错字吗?

标签: c++ csv parsing


【解决方案1】:

我看不出你的数据被裁剪的原因,但我已经稍微重构了你的代码,使用它可能更容易调试问题,如果它不只是自行消失的话。

int main()
{
    string path("D:/Audit.csv");

    ifstream input_file(path);
    if (!input_file.is_open()) 
    {
        cerr << "Could not open the file - '" << path << "'" << endl;
        exit(EXIT_FAILURE);
    }
    std::map<int, std::vector<string>> csv_contents;
    std::vector<string> items;
    string record;
    char delimiter = ';';

    int counter = 0;
    while (std::getline(input_file, record))
    {
        istringstream line(record);
        while (std::getline(line, record, delimiter))
        {
            items.push_back(record);
            cout << record << endl;
        }

        csv_contents[counter] = items;
        items.clear();
        ++counter;
    }
    return counter;
}

我已经尝试过您的代码并且(在修复分隔符后)没有问题,但我只有三行数据,所以如果是内存问题,它不太可能显示出来。

【讨论】:

  • 感谢您清理我的代码。您的代码就像一个魅力,没有更多的裁剪文本。我不确定我的版本的错误是什么。但我继续使用您的版本,现在正在为我的行实施过滤器。谢谢先生。
猜你喜欢
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 2021-11-13
  • 2016-03-22
  • 2016-10-31
  • 1970-01-01
  • 2019-08-25
相关资源
最近更新 更多