如何在没有换行符的情况下读取CSV记录"rn"?

How to read CSV record without the newline CRLF " "?

本文关键字:CSV 记录 读取 情况下 换行符      更新时间:2023-10-16

我对c++非常陌生,我一直在努力弄清楚如何将CSV文件读取为矢量。到目前为止,一切都很好,除了我不知道如何避免在每个CSV记录的末尾换行符。

下面是我的代码:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
// stream input operator overloaded to read a list of CSV fields
std::istream &operator >> (std::istream &ins, std::vector<std::string> &record)
{
    record.clear();
    // read the entire line into a string
    std::string line;
    std::getline(ins, line);
    // using a stringstream to separate the fields out of the line
    std::stringstream ss(line);
    std::string field;
    while (std::getline(ss, field, ';'))
    {
        // add the converted field to the end of the record
        record.push_back(field);
    }
    return ins;
}
// stream input operator overloaded to read a list of CSV fields
std::istream &operator >> (std::istream &ins, std::vector<std::vector<std::string>> &data)
{
    data.clear();
    // add results from record into data
    std::vector<std::string> record;
    bool empty;
    while (ins >> record)
    {
        // check if line has a price
        for (unsigned i = 0; i < record.size(); i++)
        {
            std::stringstream ss(record[2]);
            int price;
            if (ss >> price)
            {
                empty = true;
            }
            else
            {
                empty = false;
            }
        }
        if (empty == true)
        {
            data.push_back(record);
        }
    }
    return ins;
}
int main()
{
    // bidemensional vector for storing the menu
    std::vector<std::vector<std::string>> data;
    // reading file into data
    std::ifstream infile("test.csv");
    infile >> data;
    // complain if theres an error
    if (!infile.eof())
    {
        std::cout << "File does not excist." << std::endl;
        return 1;
    }
    infile.close();

    for (unsigned m = 0; m < data.size(); m++)
    {
        for (unsigned n = 0; n < data[m].size(); n++)
        {
            std::string recordQry;
            recordQry += "'" + data[m][n] + "', ";
            std::cout << recordQry;
        }
        std::cout << std::endl;
    }
    return 0;
}

test.csv包含:

CODE;OMSCHRIJVING; PRIJS ;EXTRA;SECTION
A1;Nasi of Bami a la China Garden; 12,00 ;ja;4
A2;Tjap Tjoy a la China Garden; 12,00 ;ja;1
A3;Tja Ka Fu voor twee personen; 22,50 ;ja;1

try:

while (std::getline(ss, field, ';'))
{
    // add the converted field to the end of the record
    record.push_back(field.erase(s.find('r'));
}
//record.push_back(field.erase(s.find('r'));
return ins;

我本来打算删除我的答案,但决定重新提交一个,因为不管关于getline的所有事实都在飞,你确实知道你有这个问题。在另一个回答的评论中,我注意到你提到它最初是一个excel文件。好吧,至少在某些情况下,微软以rn结束他们的产品线。这是原因吗?getline仍然会放弃n,但你仍然会有回车。如果是这种情况,您将需要使用我之前分享的一种方法。我希望我已经救赎了自己。

我检查了使用rn写入文件,是的,它将离开r。我在调试中观看了它,甚至当我再次使用getline来提取它在字符串中留下的值时。当它打印到控制台时,它会将光标移到第一个字母的下方。

微软显然使用这种样式是为了向后兼容旧的机器,旧的机器需要两个单独的功能——一个是将标题返回到左边的空白,一个是卷起纸张。这听起来像你正在经历的行为吗?