4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

boost::tokenizerでcsvをパースしてみました。

Last updated at Posted at 2017-12-31

C++における定番のcsvパーサーは?

C++csvファイルをパースするライブラリってないのかと思ってぐぐってみたが、
あまり「コレだ!」というのがなかったのでとりあえずboostを試してみました。

こんな感じになりました。

# include <iostream>
# include <fstream>
# include <vector>
# include <string>
# include <boost/tokenizer.hpp>


std::vector < std::vector< std::string > > parse_csv(const char* filepath)
{
    std::vector< std::vector< std::string > > cells;
    std::string line;
    std::ifstream ifs(filepath);

    // csvを走査
    while (std::getline(ifs, line)) {
        
        std::vector< std::string > data;
        
        // 1行を走査
        boost::tokenizer< boost::escaped_list_separator< char > > tokens(line);
        for (const std::string& token : tokens) {
            data.push_back(token);
        }

        // 1行読み込んだ結果を入れる
        cells.push_back(data);
    }

    return cells;
}



int main(void)
{
    const auto cells = parse_csv("test.csv");
    for (const auto& rows : cells) {
        for (const auto& cell : rows) {
            std::cout << "<" << cell << "> " << std::endl;
        }
        std::cout << std::endl;
    }

    return 0;
}

実行環境

OS: ubuntu16.04LTS
コンパイラ: clang++ 5.0.0

入力と結果

入力
2017,"hoge",,field 1
2018,"huga",,field 4649
結果
<2017> 
\<hoge> 
<> 
\<field 1> 

<2018> 
\<huga> 
<> 
\<field 4649> 

感想

エラーハンドリングなしで書いたのでめちゃくちゃ簡単でした。
まあエラーハンドリングを書いたところで大してコードは膨らまないと思いますが。

参考

http://www.boost.org/doc/libs/1_66_0/libs/tokenizer
http://kaworu.jpn.org/cpp/boost::tokenizer%E3%81%A7CSV%E3%82%92%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%82%80

4
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?