久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

帶有逗號和雙引號內的字符串的 C++ CSV 行

C++ CSV line with commas and strings within double quotes(帶有逗號和雙引號內的字符串的 C++ CSV 行)
本文介紹了帶有逗號和雙引號內的字符串的 C++ CSV 行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在用 C++ 讀取 CSV 文件,行格式如下:

I'm reading a CSV file in C++ and the row format is as such:

小學,中學,第三",主要",,中學",18, 4, 0, 0, 0

"Primary, Secondary, Third", "Primary", , "Secondary", 18, 4, 0, 0, 0

(注意空值)

當我這樣做時:

while (std::getline(ss, csvElement, ',')) {
   csvColumn.push_back(csvElement);
}

這會將第一個字符串分成不正確的部分.

This splits up the first string into pieces which isn't correct.

如何在迭代時保留字符串?我嘗試將上述方法結合起來,同時還抓取了由雙引號分隔的行,但我得到了瘋狂的結果.

How do I preserve the string when iterating? I tried to do a combination of the above and while also grabbing the lines separated by double quote but I got wild results.

推薦答案

您需要根據是否在引號之間來解釋逗號.這對于 getline() 來說太復雜了.

You need to interpret the comma depending on whether you're betwwen the quote or not. This is too complexfor getline().

解決方案是使用 getline() 讀取整行,并通過逐個字符遍歷字符串來解析該行,并維護一個指示符是否在雙引號之間.

The solution would be to read the full line with getline(), and parse the line by iterating through the string character by character, and maintaing an indicator whether you're between double quotes or not.

這是第一個原始"示例(未刪除字段中的雙引號且不解釋轉義字符):

Here is a first "raw" example (double quotes are not removed in the fields and escape characters are not interpreted):

string line; 
while (std::getline(cin, line)) {        // read full line
    const char *mystart=line.c_str();    // prepare to parse the line - start is position of begin of field
    bool instring{false};                
    for (const char* p=mystart; *p; p++) {  // iterate through the string
        if (*p=='"')                        // toggle flag if we're btw double quote
            instring = !instring;     
        else if (*p==',' && !instring) {    // if comma OUTSIDE double quote
            csvColumn.push_back(string(mystart,p-mystart));  // keep the field
            mystart=p+1;                    // and start parsing next one
        }
    }
csvColumn.push_back(string(mystart));   // last field delimited by end of line instead of comma
}

在線演示

這篇關于帶有逗號和雙引號內的字符串的 C++ CSV 行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 激情一区二区三区 | 四虎永久免费黄色影片 | 精品在线一区二区 | 四虎成人精品永久免费av九九 | 欧美91| 最新国产精品精品视频 | www.99re| 免费观看av网站 | 蜜桃视频在线观看免费视频网站www | www.99re5.com| 午夜影视 | 国产在线一区二 | 欧美美女被c | 精品视频在线观看 | www.4567| 国产欧美日韩一区二区三区在线观看 | 91福利网| 日一区二区 | 国产精品久久久久久久免费大片 | 亚洲欧美一区在线 | 91中文在线观看 | 国产一二三视频在线观看 | 亚洲精选久久 | 精品国产青草久久久久96 | 国产成人精品一区二区三区在线 | 亚洲国产欧美在线 | 成人一区二区三区在线观看 | www.久久99 | 国产精品成人一区二区三区 | 精品久久久久久红码专区 | 99精品热视频 | 99精品免费久久久久久日本 | 精品久久久久久久久久 | 欧美一区二区免费电影 | 日韩在线中文字幕 | 成人网av| 亚洲欧美日韩在线一区二区 | 国产精品久久久久久久久久久久 | 一区二区三区免费 | 成年人在线观看视频 | www97影院 |