Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created January 12, 2017 03:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/ae60e06c57b95bcffdf85f7656a983f9 to your computer and use it in GitHub Desktop.
Save komasaru/ae60e06c57b95bcffdf85f7656a983f9 to your computer and use it in GitHub Desktop.
C++ source code to read csv data.
/**
* @file csv.cpp
* @brief CSV 処理クラス
*
* @date 2017-01-10 新規作成
* @author mk-mode.com
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "csv.hpp"
using namespace std;
/**
* @brief コンストラクタ
*
* @param[in] CSVファイル(string)
* @return 真偽(bool)
* @retval true 成功
* @retval false 失敗
*/
Csv::Csv(string csv_file)
{
this->csv_file = csv_file;
}
/**
* @brief CSV データ取得
*
* @param[in] CSV ファイル名(string&)
* @param[out] CSV データ(vector<vector<string>>&)
* @param[in] デリミッタ(const char)
* @return 真偽(bool)
* @retval true 成功
* @retval false 失敗
*/
bool Csv::getCsv(vector<vector<string>>& data, const char delim)
{
// ファイルOPEN
ifstream ifs(csv_file);
if (!ifs.is_open()) return false; // 読み込み失敗
// ファイルREAD
string buf; // 1行分バッファ
while (getline(ifs, buf)) {
vector<string> rec; // 1行分ベクタ
istringstream iss(buf); // 文字列ストリーム
string field; // 1列分文字列
// 1列分文字列を1行分ベクタに追加
while (getline(iss, field, delim)) rec.push_back(field);
// 1行分ベクタを data ベクタに追加
if (rec.size() != 0) data.push_back(rec);
}
return true; // 読み込み成功
}
/**
* @file csv.hpp
* @brief CSV 読み込み処理
*
* @date 2017-01-10 新規作成
* @author mk-mode.com
*/
#ifndef INCLUDED_CSV_HPP
#define INCLUDED_CSV_HPP
#include <string>
#include <vector>
/**
* @class Csv
* @brief CSV 処理クラス
*/
class Csv
{
//! CSV ファイル名
std::string csv_file;
public:
//! コンストラクタ
Csv(std::string);
//! CSV データ取得
bool getCsv(std::vector<std::vector<std::string>>&, const char delim = ',');
};
#endif
/**
* @file read_csv.cpp
* @brief CSVファイル読込み
*
* @date 2017-01-10 新規作成
* @author mk-mode.com
*/
#include <iostream>
#include <string>
#include <vector>
#include "csv.hpp"
using namespace std;
/**
* @brief メイン処理
*
* @param[in] <none>
* @param[out] <none>
* @return リターンコード(int)
* @retval 0 正常終了
* @retval 1 異常終了
*/
int main()
{
const string csv_file = "sample.csv"; //! CSVファイル
vector<vector<string>> data; //! CSVデータ
try {
// CSVデータ取得
Csv objCsv(csv_file);
if (!objCsv.getCsv(data)) {
cout << "[ERROR] 読込み失敗!" << endl;
return 1;
}
// 内容出力
for (unsigned int row = 0; row < data.size(); row++) {
vector<string> rec = data[row]; // 1行
for (unsigned int col = 0; col < rec.size(); col++) {
cout << rec[col]; // 1列
if (col < rec.size() - 1) cout << ","; // 行末以外はカンマ出力
}
cout << endl;
}
}
catch (...) {
cout << "EXCEPTION!" << endl;
return 1;
}
return 0;
}
@tlf60
Copy link

tlf60 commented Oct 15, 2018

I downloaded this code and tried to compile with DevC++ 5.11 build April 2015. (Recently downloaded, this month)
I got lots of errors, like that the Csv class constructor was not declared - it seemed the problem was something to do with the type of the argument.
Any idea what the problem is.
Another error example was that the compiler did not like the std::vector<std::vectorstd::string>&, it seemed to read >> as an operator, not a pair of brackets.

I tried downloading a different csv reading code and got similar problems.

@khoatcao
Copy link

khoatcao commented Jun 4, 2019

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment