• C++ 读写csv文件


    1.需求

    数据库表数据->本地csv文件->数据库表数据,需要对csv文件进行读写操作,支持中文路径

    2.接口用例

    • UniTest.cpp

    #include <iostream>
    #include "CSVFile.h"
    
    using namespace std;
    int main()
    {
        CSVFile dataFile("data.csv",",");
    
        //Cells生成Line
        vector<string> vecCells = {"1","2","3","4","5"};
        std::string strLine;
        dataFile.MakeLine(vecCells, strLine);
        cout << "生成的csv行数据 :" << strLine << endl;
    
        //Line解析成Cells
        vector<string> vecNewCells;
        dataFile.ParseLine(strLine, vecNewCells);
    
    
        //向文件写入Line
        dataFile.AppendLine(strLine);
        dataFile.AppendLine(strLine);
    
        //从文件读取所有Line
        vector<string> vecLines;
        dataFile.ReadLines(vecLines);
        cout << "读取多行数据内容 :" << endl;
        for (vector<string>::const_iterator iter = vecLines.begin();iter != vecLines.end();++iter)
        {
            cout << *iter << endl;
        }
    
        //向文件追加多个Line
        dataFile.AppendLines(vecLines);
    
        //清除文件内容,再写入多个Line
        dataFile.WriteLines(vecLines);
    
    
        std::string strPause;
        cin >> strPause;
        return 0;
    }

    3.接口及实现

    • CSVFile.h

     1 #pragma once
     2 
     3 #include <vector>
     4 #include <string>
     5 
     6 using std::vector;
     7 using std::string;
     8 
     9 class CSVFile 
    10 {
    11 public:
    12     CSVFile(std::string strFileName, std::string strDelimeter);
    13     ~CSVFile();
    14 
    15     //解析csv行到cells
    16     void ParseLine(const std::string &strLine, std::vector<std::string> vecCells);
    17     //从cells生成csv行
    18     void MakeLine(const std::vector<std::string> &vecCells, std::string &strLine);
    19     //追加一行数据
    20     void AppendLine(std::string strLine);
    21     //清空后写多行数据
    22     void WriteLines(const std::vector<std::string> &vecLines);
    23     //追加多行数据
    24     void AppendLines(const std::vector<std::string> &vecLines);
    25     //读取文件数据
    26     void ReadLines(std::vector<std::string> &vecLines);
    27 private:
    28     //文件名
    29     std::string m_strFileName;
    30     //分隔符
    31     std::string m_strDelimeter;
    32 };
    • CSVFile.cpp

     1 #include <fstream>
     2 #include "CSVFile.h"
     3 
     4 using namespace std;
     5 
     6 CSVFile::CSVFile(std::string strFileName, std::string strDelimeter)
     7 {
     8     m_strFileName = strFileName;
     9     m_strDelimeter = m_strDelimeter;
    10 }
    11 
    12 CSVFile::~CSVFile()
    13 {
    14 
    15 }
    16 
    17 void CSVFile::ParseLine(const std::string &strLine, std::vector<std::string> vecCells)
    18 {
    19     std::string str = strLine;
    20     while (str.find(m_strDelimeter.c_str()) != std::string::npos)
    21     {
    22         unsigned int nPos = str.find(m_strDelimeter.c_str());
    23         std::string strCell = str.substr(0, nPos);
    24         str = str.substr(nPos + 1, str.size());
    25         vecCells.push_back(strCell);
    26     }
    27     vecCells.push_back(str);
    28 }
    29 
    30 void CSVFile::MakeLine(const std::vector<std::string> &vecCells, std::string &strLine)
    31 {
    32     for (vector<string>::const_iterator iter = vecCells.begin(); iter != vecCells.end(); ++iter)
    33     {
    34         std::string strCell = *iter;
    35         strLine += strCell;
    36         //最后一个不带,
    37         if ((iter + 1) != vecCells.end())
    38         {
    39             strLine += m_strDelimeter;
    40         }
    41     }
    42 }
    43 
    44 void CSVFile::AppendLine(std::string strLine)
    45 {
    46     ofstream ofs;
    47     std::locale loc = std::locale::global(std::locale(""));
    48     ofs.open(m_strFileName.c_str(), ios::out | ios::app);
    49     std::locale::global(loc);
    50     ofs << strLine << endl;
    51     ofs.close();
    52 }
    53 
    54 void CSVFile::AppendLines(const std::vector<std::string> &vecLines)
    55 {
    56     ofstream ofs;
    57     std::locale loc = std::locale::global(std::locale(""));
    58     ofs.open(m_strFileName.c_str(), ios::out | ios::app);
    59     std::locale::global(loc);
    60     for (std::vector<std::string>::const_iterator iter = vecLines.begin(); iter != vecLines.end(); ++iter)
    61     {
    62         ofs << *iter << endl;
    63     }
    64     ofs.close();
    65 }
    66 
    67 void CSVFile::WriteLines(const std::vector<std::string> &vecLines)
    68 {
    69     ofstream ofs;
    70     std::locale loc = std::locale::global(std::locale(""));
    71     ofs.open(m_strFileName.c_str(), ios::out | ios::trunc);
    72     std::locale::global(loc);
    73     for (std::vector<std::string>::const_iterator iter = vecLines.begin(); iter != vecLines.end(); ++iter)
    74     {
    75         ofs << *iter << endl;
    76     }
    77     ofs.close();
    78 }
    79 
    80 void CSVFile::ReadLines(std::vector<std::string> &vecLines)
    81 {
    82     ifstream ifs;
    83     std::locale loc = std::locale::global(std::locale(""));
    84     ifs.open(m_strFileName.c_str(), ios::in);
    85     std::locale::global(loc);
    86 
    87     std::string strLine;
    88     while (getline(ifs, strLine))
    89     {
    90         vecLines.push_back(strLine);
    91     }
    92 }
  • 相关阅读:
    bzoj 4237 稻草人
    bzoj 4537 最小公倍数
    POJ 2763 Housewife Wind(树链剖分)(线段树单点修改)
    HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)
    spoj 913 Query on a tree II (倍增lca)
    spoj 375 Query on a tree (树链剖分)
    hiho一下第133周 2-SAT·hihoCoder音乐节(2-SAT)(强连通)
    hiho一下第131周 后缀自动机二·重复旋律8(循环相似子串)
    hiho一下第130周 后缀自动机二·重复旋律7
    hiho一下第129周 后缀自动机二·重复旋律6
  • 原文地址:https://www.cnblogs.com/SwiftChocolate/p/13460009.html
Copyright © 2020-2023  润新知