LoginSignup
0
0

More than 1 year has passed since last update.

C++CSVクラス

Last updated at Posted at 2022-12-04

個人メモ用
切り貼りしたコードでまだ未デバッグ。
一応継承すること想定

csv_class.cpp

#include <fstream>
#include <string>
#include <vector>

class CSVFile
{
public:
    CSVFile(const std::string& filename, char delimiter = ',');

    // ファイルを開く
    bool open();

    // ファイルを閉じる
    void close();

    // ファイルから1行読み込む
    bool readRow(std::vector<std::string>& row);

    // ファイルに1行書き込む
    bool writeRow(const std::vector<std::string>& row);

private:
    std::string m_filename;  // ファイル名
    char m_delimiter;        // 区切り文字
    std::fstream m_file;     // ファイルストリーム
};

CSVFile::CSVFile(const std::string& filename, char delimiter)
    : m_filename(filename), m_delimiter(delimiter), m_file()
{
}

bool CSVFile::open()
{
    // ファイルを開く
    m_file.open(m_filename, std::ios::in | std::ios::out | std::ios::trunc);
    if (!m_file.is_open())
    {
        return false;
    }

    return true;
}

void CSVFile::close()
{
    if (m_file.is_open())
    {
        // ファイルを閉じる
        m_file.close();
    }
}

bool CSVFile::readRow(std::vector<std::string>& row)
{
    if (!m_file.is_open())
    {
        // ファイルが開かれていない
        return false;
    }

    std::string line;
    if (!std::getline(m_file, line))
    {
        // 行が読み込めない
        return false;
    }

    // 1行をカンマで分割して、行の要素として格納する
    std::string token;
    std::istringstream tokenStream(line);
    while (std::getline(tokenStream, token, m_delimiter))
    {
        row
.push_back(token);
    }

    return true;
}

bool CSVFile::writeRow(const std::vector<std::string>& row)
{
    if (!m_file.is_open())
    {
        // ファイルが開かれていない
        return false;
    }

    for (std::vector<std::string>::const_iterator it = row.begin(); it != row.end(); ++it)
    {
        if (it != row.begin())
        {
            // 2番目以降の列の前に区切り文字を挿入する
            m_file << m_delimiter;
        }

        // 列の値を書き込む
        m_file << *it;
    }

    // 行末に改行を挿入する
    m_file << std::endl;

    return true;
}
main.cpp
#include <iostream>
#include <vector>

int main()
{
    CSVFile file("myfile.csv");

    // ファイルを開く
    if (!file.open())
    {
        std::cerr << "Failed to open file." << std::endl;
        return 1;
    }

    // ファイルから1行読み込む
    std::vector<std::string> row;
    while (file.readRow(row))
    {
        // 読み込んだ行を出力する
        std::cout << "Read row:";
        for (std::vector<std::string>::const_iterator it = row.begin(); it != row.end(); ++it)
        {
            std::cout << " " << *it;
        }
        std::cout << std::endl;

        // 行をクリアする
        row.clear();
    }

    // ファイルに1行書き込む
    std::vector<std::string> writeRow;
    writeRow.push_back("abc");
    writeRow.push_back("123");
    file.writeRow(writeRow);

    // ファイルを閉じる
    file.close();

    return 0;
}
0
0
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
0
0