LoginSignup
1
2

More than 5 years have passed since last update.

C++でBootを使わずにカンマで区切って配列にいれる

Last updated at Posted at 2015-01-22

はじめに

今回は外部のテキストデータ(txt)を取得後、カンマで区切られている単語を配列に入れていくプログラムを作りました。
bootとか使う方法はネットにたくさん落ちてたんですけど、自分の環境下にbootを入れていないこともあって使わないで実装したいと思ったので作ってみました。

追記あり。

hoge.txt

hoge.txt
"2015/01/01","00:00:00.321","Japan","32.3","9099"
"2015/01/01","00:00:01.213","Japan","33.5","9092"
"2015/01/01","00:00:01.345","China","31.3","9399"
"2015/01/01","00:00:02.390","Japan","34.6","9129"
"2015/01/01","00:00:02.908","China","30.4","8899"
"2015/01/01","00:00:03.001","Japan","35.1","1299"

プログラム

hoge.cpp
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int twoArrayOfDepth(string tDF,string com,int *count,int *count2);

int main(){
    string str,str2;
    string textDataFile = "hoge.txt";
    string com = ",";
    int number,number2,number3,c1,c2,i,j;

    twoArrayOfDepth(textDataFile,com,&c1,&c2);

    string** textData = new string*[c1];
    for (i=0; i<c1; i++)textData[i] = new string[c2];

    ifstream fileText(textDataFile);
    if(fileText.fail())return EXIT_FAILURE;
    number = 1;
    while (getline(fileText,str)) {
        number2 = 1;
        number3 = 0;
        string::size_type idx = str.find(com);
        while (idx != string::npos) {
            if (number2==1) {
                textData[number-1][number2-1] = str.substr(number3,idx - number3).c_str();
                number3 = idx;
                number2++;
                idx = str.find(com, idx + 1);
                textData[number-1][number2-1] = str.substr(number3+1,idx - number3-1).c_str();

            }else{
                idx = str.find(com, idx + 1);
                textData[number-1][number2-1] = str.substr(number3+1,idx - number3-1).c_str();
            }
            number3 = idx;
            number2++;
        }
        number++;
    }

    fileText.close();

    for (i=0; i<c1; i++)for (j=0; j<c2; j++)cout<<textData[i][j]<<endl;
    return 0;
}

int twoArrayOfDepth(string tDF,string com,int *count,int *count2){
    string str2;
    ifstream fileText2(tDF);
    if(fileText2.fail())return EXIT_FAILURE;

    *count = 0;
    while (getline(fileText2,str2)) {
        *count2 = 1;
        string::size_type idx2 = str2.find(com);
        while (idx2 != string::npos) {
            idx2 = str2.find(com, idx2 + 1);
            *count2 = *count2 + 1;
        }
        *count = *count + 1;
    }
    fileText2.close();
    return 0;
}

実行結果

programExecution.png

解説?

twoArrayOfDepth()

2次元配列の深さ(要素数)を決定する関数です。

twoArrayOfDepth()
int twoArrayOfDepth(string tDF,string com,int *count,int *count2){
    string str2;
    ifstream fileText2(tDF);
    if(fileText2.fail())return EXIT_FAILURE;

    *count = 0;
    while (getline(fileText2,str2)) {
        *count2 = 1;
        string::size_type idx2 = str2.find(com);
        while (idx2 != string::npos) {
            idx2 = str2.find(com, idx2 + 1);
            *count2 = *count2 + 1;
        }
        *count = *count + 1;
    }
    fileText2.close();
    return 0;
}

count:行数
count2:カンマ(,)で区切られている単語数

追記

コメント欄でもっと良いプログラムを書いてくれた方がいますのでそちらを参考にしていただいた方がいいかもしれませんm(_ _)m!

注:$c++ hoge.cppでコンパイルすると次のようなWarningが出ます。
warning.png

ただし、これは$c++ hoge.cppではなく、$clang++ -std=c++11 hoge.cppでコンパイルするとWarningなしで行えます。
参考:C++ auto type specifier is a C++11 extension

1
2
2

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
1
2