久久久久久久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在线播放国产 | 日韩av在线不卡 | 国产黄视频在线播放 | 一区二区三区在线观看视频 | 精品免费国产视频 | 成人超碰 | 香蕉大人久久国产成人av | 精品一区二区三区四区外站 | 亚洲综合无码一区二区 | 又黄又爽的网站 | 久久精品一级 | avav在线看| 欧美1页 | 91视频中文 | 一本岛道一二三不卡区 | 涩爱av一区二区三区 | av在线三级 | 日一区二区 | 日本韩国欧美在线观看 | 精品99在线 | 久久伊人精品 | 国产一区二区三区四区五区3d | 国产精品一区二区久久 | 噜噜噜色网 | 超碰人人人 | 亚洲一区综合 | 欧美一级片在线 | 毛片免费视频 | 久久一二| 伊人久久综合 | 亚洲精品白浆高清久久久久久 | av黄色在线 | 天天噜天天干 | 国产精品久久久久久久毛片 | 国产一区二区三区 | 天天干天天草 | 在线亚洲精品 | 欧产日产国产精品视频 | 紧缚调教一区二区三区视频 |