【CSV文件】CSV文件內容讀取


CSV(逗號分隔值文件格式)

         逗號分隔值(Comma-Separated Values,CSV,有時也稱為字符分隔值,因為分隔字符也可以不是逗號),其文件以純文本形式存儲表格數據(數字和文本)。純文本意味着該文件是一個字符序列,不含必須像二進制數字那樣被解讀的數據。CSV文件由任意數目的記錄組成,記錄間以某種換行符分隔;每條記錄由字段組成,字段間的分隔符是其它字符或字符串,最常見的是逗號或制表符。通常,所有記錄都有完全相同的字段序列。

CSV文件格式的通用標准並不存在,但是在RFC 4180中有基礎性的描述。使用的字符編碼同樣沒有被指定,但是7-bit ASCII是最基本的通用編碼。

這種文件格式經常用來作為不同程序之間的數據交互的格式。

具體文件格式:每條記錄占一行 以逗號為分隔符 逗號前后的空格會被忽略 字段中包含有逗號,該字段必須用雙引號括起來 字段中包含有換行符,該字段必須用雙引號括起來 字段前后包含有空格,該字段必須用雙引號括起來 字段中的雙引號用兩個雙引號表示 字段中如果有雙引號,該字段必須用雙引號括起來 第一條記錄,可以是字段名

John,Doe,120 jefferson st.,Riverside, NJ, 08075
Jack,McGinnis,220 hobo Av.,Phila, PA,09119
"John ""Da Man""",Repici,120 Jefferson St.,Riverside, NJ,08075
Stephen,Tyler,"7452 Terrace ""At the Plaza"" road",SomeTown,SD, 91234
,Blankman,,SomeTown, SD, 00298
"Joan ""the bone"", Anne",Jet,"9th, at Terrace plc",Desert City,CO,00123

 

#include <iostream>
#include <fstream>

ifstream file(m_strFilePath);
    std::string row;
    vector<string> infRow;
    getline(file, row);//讀取第一行
    MySplit(row, infRow); //獲取每列的內容

    while (file.good())
    {
        //讀取每一行
        getline(file, row);
        CStringA strRow = row.c_str();
        strRow.Replace("\\", "\\\\");
        strRow.Replace("'", "\\'");
        row = strRow;
        infRow.clear();
        MySplit(row, infRow);
                //...
    }




char *m_pcTemp;
int m_iMaxTempLength;

m_pcTemp(NULL), m_iMaxTempLength(10240)

//csv
void PushInTemp(const char *pcCursor, int iLen)
{
    if (iLen >= m_iMaxTempLength)
    {
        m_iMaxTempLength = iLen * 2;
        if (m_pcTemp)
        {
            delete[] m_pcTemp;
            m_pcTemp = nullptr;
        }
        
        m_pcTemp = new char[m_iMaxTempLength];
    }
    if (iLen > 0)
    {
        if (!m_pcTemp)
        {
            m_pcTemp = new char[m_iMaxTempLength];
        }
            
        memcpy(m_pcTemp, pcCursor, iLen);
    }

    if(m_pcTemp)
        m_pcTemp[iLen] = '\0';
}

//csv
void MySplit(std::string &row, vector<string> &infRow)
{
    const char *pcCursor = row.c_str();
    const char *pcComma = NULL;
    const char *pcQuot = NULL;

    do
    {
        if (0 == row.size())
        {
            break;
        }
        pcComma = strchr(pcCursor, ',');
        pcQuot = strchr(pcCursor, '"');

        if (NULL == pcComma && NULL == pcQuot)
        {
            infRow.push_back(pcCursor);
            break;
        }
        if (NULL == pcQuot)
        {
            int iLen = (int)pcComma - (int)pcCursor;

            PushInTemp(pcCursor, iLen);
            infRow.push_back(m_pcTemp);
            pcCursor += iLen + 1;
        }
        else if (NULL == pcComma)
        {
            const char *pcLastQuot = strrchr(pcCursor, '"');
            int iLen = (int)pcLastQuot - (int)pcQuot - 1;

            PushInTemp(pcQuot + 1, iLen);
            infRow.push_back(m_pcTemp);
            break;
        }
        else
        {
            int iCommaPos = (int)pcComma;
            int iQuotPos = (int)pcQuot;

            if (iCommaPos < iQuotPos)
            {
                int iLen = (int)pcComma - (int)pcCursor;
                PushInTemp(pcCursor, iLen);
                infRow.push_back(m_pcTemp);
                pcCursor += iLen + 1;
            }
            else
            {
                const char *pcNextQuot = NULL;
                int iMove = (int)pcQuot - (int)pcCursor + 1;
                std::string strQuotData;

                pcCursor += iMove;

                do
                {
                    pcNextQuot = strchr(pcCursor, '"');
                    if (NULL == pcNextQuot)
                    {
                        goto end;
                    }
                    if (*(pcNextQuot + 1) == '"')
                    {
                        int iLen = (int)pcNextQuot - (int)pcCursor;
                        PushInTemp(pcCursor, iLen);
                        strQuotData += m_pcTemp;
                        pcCursor = pcNextQuot + 2;
                        strQuotData += '"';
                    }
                    else
                    {
                        int iLen = (int)pcNextQuot - (int)pcCursor;
                        PushInTemp(pcCursor, iLen);
                        strQuotData += m_pcTemp;
                        infRow.push_back(strQuotData);
                        pcCursor += iLen + 1;
                        if (*pcCursor == ',')
                        {
                            ++pcCursor;
                        }
                        break;
                    }
                } while (1);
            }
        }
        if (*pcCursor == '\0')
        {
            break;
        }
    } while (1);

end:
    return;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM