久久久久久久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 ()?環形?)
主站蜘蛛池模板: www.五月婷婷| 久久艹av| 久久怡红院 | 男男成人高潮片免费网站 | 日韩成人在线播放 | 国产一区精品在线 | 日韩欧美一级片 | 亚洲精品一二区 | 成人免费视频大全 | av网站免费观看 | 人人草人人草 | 欧美一区二区视频在线观看 | 欧美激情综合五月色丁香 | 久久久精品一区二区 | 色黄大色黄女片免费中国 | 欧美视频一区二区三区 | 日韩在线视频一区 | 一区二区三区四区精品 | av一级在线 | 国产免费黄色片 | 久久久黄色| 欧美一级黄色片 | 精品一区二区三区在线观看 | 成人免费在线播放 | 深夜免费福利 | 免费观看一级毛片 | 福利网站在线观看 | 永久在线 | 午夜伦理福利 | 不卡视频在线观看 | 亚洲毛片视频 | www.青青草 | 精东影业一区二区三区 | 999久久久国产精品 亚洲黄色三级 | 大香蕉毛片| 精品一区二区三区中文字幕 | 欧美影院一区 | 久操福利视频 | 国产aaaaaa | 欧美精品一区二区在线观看 | 免费黄色小网站 |