2017-09-03 9 views
-3

私は25行で異なる変数を持つASCIIデータファイルを持っています。私は各構造の上にループを作りたいと思っていますし、すべての構造に対して要素は配列に追加されます。たとえば、構造の最初の桁または数字は1,2,3などのイベント番号になり、2番目の数字はそのイベントの時刻(Linuxの時刻形式)になります。ここに繰り返しファイルのサンプルがあります。特定の構造のASCIIファイルを25行ごとに繰り返す方法

1 1481492919 298362 
    1  936 642 618 1346 2648  0 103 1651  69  76  7  0 
    1 63 58 43 63 43 0 59 54 21 45 80 66 49 38 
    1 50 65 39 67 119 0 87 47 79 78 50 73 24 35 
    1 37 48 44 58 49 58 45 66 61 55 86 138 80 43 
    1 32 0 45 95 49 54 57 62 42 55 107 162 67 40 
    1 1688 1678 1675 1674 1670 1684 1707 1675 1687 1683 1686 1695 1693 1690 
    1 2047 2047 2047 2047 1808 2047 2047 2047 2047 2047 2047 2047 648 2047 
    1 1776 1770 1776 1797 1799 1790 1774 1768 1791 1784 1800 1789 1775 1747 
    1 2047 2047 2047 2047 2047 2047 2047 2047 2047 2047 2047 2047 2047 2047 
    1 108 155 97 84 100 109 98 90 292 
    1 2047 581 2047 2047 2047 2047 2047 2047 2047 
    1 45 44 175 60 50 55 48 39 22 
    1 2047 2047 2047 610 2047 2047 2047 2047 2047 
    1 65 77 53 78 52 53 46 134 40 
    1 2047 2047 2047 2047 2047 2047 2047 2047 2047 
    1 0 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 
    1 1 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000 
    1 0 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 
    1 0 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 

screenshot

+0

あなたがすでに行ったことを示してください。 – Serge

+0

[std :: fstream](http://en.cppreference。com/w/cpp/io/basic_fstream)クラステンプレートには、ジョブに必要なツールが用意されています。 – Ron

+0

@Sergeはい私は、\t ifstream f(aFile)でターミナルでasciiを読み込んで印刷できました。 \t文字列。 \t while(!f.eof()){ \t getline(f、line); \t if(line [0] == 'e' || line [0] == 'f' || line [1] == 'f' || line [1] == 'n' || line [2] == 'f') \t \t cout << "IGNORE LINE \ n"; \t else \t cout << line << "\ n"; \t} – Gunn

答えて

1

典型的なアプローチは、以下の構造を有する各テキスト行をモデル化することです。これはレコードと呼ばれます。

ファイル形式には異なる種類のレコードがあるようですので、異なる構造が必要です。

各構造内で、フォーマットされたストリームからメンバーを読み取るためにオーバーロードoperator>>をオーバーロードします。これは、単純なプログラミングを可能にする:

Record1 r; 
my_file >> r; 

あなたはforループに入力文を置くことができます。

std::vector<Record> block_of_data; 
Record r; 
for (unsigned int i = 0U; i < 5; ++i) 
{ 
    my_file >> r; 
    block_of_data.push_back(r); 
} 

することはできモデル構造内のレコードを使用してファイル。
コンパイル時にデータ量がわからないことがあるので、配列よりもstd::vectorを使用することをお勧めします。

編集1:例
データの最初の行には2つの数字が含まれています。それでは、これをモデル化するクラスを作成してみましょう:

class Record1 
{ 
    public: 
    int x; // First number 
    int y; // Second number; 
    // Overload operator>> to read in the numbers 
    friend std::istream& operator>>(std::istream& input, Record1& r); 
}; 

std::istream& operator>>(std::istream& input, Record1& r) 
{ 
    input >> r.x; 
    input >> r.y; 
    input.ignore(10000, '\n'); // Eat remaining newline if there is one. 
    return input; 
} 

ご入力ループは次のようになります。あなたは次のレコードが12個の番号が含まれているので、それがどのように見えるかもしれ

int item_number = 0; 
Record1 r1; 
while (data_file >> item_number) // Read in first number on the text line. 
{ 
    // Read the first line (record) 
    data_file >> r1; 
    // ... 
} 

class Record2 
{ 
    public: 
    std::vector<int> data; 
    friend std::istream& operator>>(std::istream& input, Record2& r); 
}; 

std::istream& operator>>(std::istream& input, Record2& r) 
{ 
    // another technique: read in as a line of text, then parse the string. 
    std::string text; 
    std::getline(input, text); 
    std::istringstream text_stream(text); 
    int number = 0; 
    while (text_stream >> number) 
    { 
     data.push_back(number); 
    } 
    return input; 
} 

ファイルの読み込みループ:

Record1 r1; 
Record2 r2; 
int item_number; 
while (data_file >> item_number) 
{ 
    data_file >> r1; // Read first record; 
    data_file >> item_number; 
    data_file >> r2; // Read second record; 
    // etc. 
} 

したい場合は、あなたが一つのクラスに行をカプセル化することができます:

class Data_Item 
{ 
    public: 
    int   item_number; 
    Record1  r1; 
    Record2  r2; 
    //... 
    friend std::istream& operator>>(std::istream& input, Data_Item& d); 
}; 

std::istream& operator>>(std::istream& input, Data_Item& d) 
{ 
    // Note: first number on row is item number. 
    // Row 1 
    input >> d.item_number; 
    input >> d.r1; 

    // Row 2 
    input >> d.item_number; 
    input >> d.r2; 
    //... 
    return input; 
} 

あなたのファイルはの容器で、それでは、そのようにそれを読んで聞かせ:オーバーロードの詳細については

std::vector<Data_Item> database; 
Data_item d; 
while (data_file >> d) 
{ 
    data_file >> d; 
    database.push_back(d); 
} 

入力演算子、 "C++過負荷演算子ストリームの例"をインターネットで検索します。

私はoperator>>operator<<をオーバーロードの説明と例が含まれているスコット・マイヤーの効果的なC++シリーズを、取得をお勧めします。

+0

こんにちはトーマス、私を許してくださいが、私はプロコーダーではなく、ただ学びたいと思っています。あなたの説明は単に私の頭の中に入っていません。 – Gunn

+0

@ガン:私の答えで私の**編集1 **を参照してください。 –

関連する問題