Top > C/C++あれこれ > Excel仕様のCSVファイルの読み込みと表示
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 
 
 
 
 
 
 
-
|
-
|
|
!
|
|
|
-
-
|
!
!
|
|
!
 
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
 
#include "CSVReader.h"
 
int main(void) {
    std::fstream r("c:\\okinawa.csv", std::ios::in);
    if( !r.is_open() ) {
        cerr << "Open Error! :" << endl;
        return -1;
    }
 
    CSVReader csv(r);
    vector<string> tokens;
    while( !csv.Read(tokens) ) {
        for( unsigned int i=0; i<tokens.size(); i++ ) {
            cout << "[" << tokens[i].c_str() << "]" << endl;
        }
    }
    csv.Close();
    return 0;
}
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
-
!
 
 
 
 
 
-
!
 
 
 
 
 
-
!
 
 
-
!
 
-
|
|
|
-
|
!
|
|
!
 
-
|
-
|
!
|
|
!
 
-
|
|
-
-
|
-
|
!
!
|
-
|
|
-
|
|
-
|
|
-
|
|
|
|
|
-
|
!
!
-
|
|
-
|
!
!
!
|
|
!
 
 
 
 
 
-
-
|
|
!
|
!
 
/**
 * CSVファイル読み込みクラス
 * @author      台北猫々
 * @version     CVS $Id: CSVReader.cpp,v 1.1 2008/03/26 12:45:24 tamamo Exp $
 * @license     BSD license:
 * Copyright (c) 2008, Taipei Cat Project
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Taipei Cat Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#include "CSVReader.h"
 
CSVReader::CSVReader(fstream& stream):
SEPARATOR(DEFAULT_SEPARATOR),
QUOTE(DEFAULT_QUOTE_CHARACTER),
pstream(&stream)
{
}
 
CSVReader::CSVReader(fstream& stream, const char sep):
SEPARATOR(sep),
QUOTE(DEFAULT_QUOTE_CHARACTER),
pstream(&stream)
{
}
 
CSVReader::CSVReader(fstream& stream, const char sep, const char quo):
SEPARATOR(sep),
QUOTE(quo),
pstream(&stream)
{
}
 
CSVReader::~CSVReader(void)
{
}
 
int CSVReader::Read(vector<string>& tokens) {
    tokens.clear();
 
    string nextLine;
    if( GetNextLine(nextLine)<=0 ) {
        return -1;
    }
    Parse(nextLine, tokens);
    return 0;
}
 
int CSVReader::GetNextLine(string& line) {
 
    if( !pstream || pstream->eof() ) {
        return -1;
    }
    std::getline( *pstream, line );
    return (int)line.length();
}
 
int CSVReader::Parse(string& nextLine, vector<string>& tokens) {
    string token;
    bool interQuotes = false;
    do {
        if (interQuotes) {
            token += '\n';
            if (GetNextLine(nextLine)<0) {
                break;
            }
        }
        
        for (int i = 0; i < (int)nextLine.length(); i++) {
 
            char c = nextLine.at(i);
            if (c == QUOTE) {
                   if( interQuotes
                       && (int)nextLine.length() > (i+1)
                       && nextLine.at(i+1) == QUOTE ){
                       token += nextLine.at(i+1);
                       i++;
                   }else{
                       interQuotes = !interQuotes;
                       if(i>2 
                           && nextLine.at(i-1) != SEPARATOR
                           && (int)nextLine.length()>(i+1) 
                           && nextLine.at(i+1) != SEPARATOR
                       ){
                           token += c;
                       }
                   }
            } else if (c == SEPARATOR && !interQuotes) {
                tokens.push_back(token);
                token.clear();
            } else {
                token += c;
            }
        }
    } while (interQuotes);
    tokens.push_back(token);
    return 0;
}
 
/**
 * ファイルストリームをクローズします。
 * @return 0:正常 -1:異常
 */
int CSVReader::Close(void) {
    if(pstream) {
        pstream->close();
        pstream = NULL;
    }
    return 0;
}
Everything is expanded.Everything is shortened.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
!
|
|
|
/**
 * CSVファイル読み込みクラス
 * @author      台北猫々
 * @version     CVS $Id: CSVReader.h,v 1.1 2008/03/26 12:45:24 tamamo Exp $
 * @license     BSD license:
 * Copyright (c) 2008, Taipei Cat Project
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Taipei Cat Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
#ifndef _CSVREADER_H__
#define _CSVREADER_H__
 
#include <string>
#include <vector>
#include <fstream>
using namespace std;
 
#define DEFAULT_SEPARATOR    ','
#define DEFAULT_QUOTE_CHARACTER    '"'
 
class CSVReader
{
public:
 
	/**
	 * コンストラクタ
	 * @param stream ファイルストリーム
	 * @comment セパレータ(,), エンクオート(")
     */
    CSVReader(fstream& stream);
 
    /**
	 * コンストラクタ
	 * @param stream ファイルストリーム
	 * @param sep セパレータ
 	 * @comment エンクオート(")
	 */
    CSVReader(fstream& stream, const char sep);
 
    /**
	 * コンストラクタ
	 * @param stream ファイルストリーム
	 * @param sep セパレータ
 	 * @param quo エンクオート
	 */
    CSVReader(fstream& stream, const char sep, const char quo);
 
    /**
	 * デストラクタ
	 */
    virtual ~CSVReader(void);
 
    /**
	 * CSVファイルを1行読み込んで、分割して配列で返します。
	 * @param tokens トークン(OUT)
	 * @return 0:正常 -1:EOF
	 */
    int Read(vector<string>& tokens);
 
    /**
	 * ファイルストリームをクローズします。
	 * @return 0:正常 -1:異常
	 */
    int Close(void);
 
private:
 
    /**
	 * ファイルから1行読み込みます。
	 * @param line 行データ
	 * @return >=0:読み込んだデータ長 -1:EOF
	 */
    int GetNextLine(string& line);
 
    /**
	 * データをパースします。
	 * @param nextLine 行データ
	 * @param tokens パースしたデータの配列(OUT)
	 * @return 0
	 */
    int Parse(string& nextLine, vector<string>& tokens);
 
    std::fstream* pstream;
    char SEPARATOR;
    char QUOTE;
 
};
 
#endif

添付ファイル: fileCSVReader.src.zip 775件 [詳細]

リロード   新規 編集 凍結解除 差分 添付 複製 名前変更   ホーム 一覧 単語検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
Last-modified: 2012-08-08 (水) 23:25:08 (2925d)