2017-09-03 26 views
-3

我有一个ASCII数据文件,它有25行不同的变量,重复。我想对每个结构进行循环,并且对于每个结构元素都将添加到数组中。例如,结构中的第一个数字或数字将是从1,2,3,...等等开始的事件编号,第二个数字将是该事件的时间(以Linux时间格式)。这里是重复文件的示例。如何读取具有每25行重复特定结构的ASCII文件?

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 yes我能够读取并打印终端中的ascii文件,文件名为\t ifstream f(aFile); \t字符串行; \t while(!f.eof()){ \t getline(f,line); 如果(line [0] =='e'|| line [0] ==' 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:示例
数据的第一行包含两个数字。所以,让我们创建一个类模型:

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

你输入回路可能看起来像:

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; 
    // ... 
} 

你下一个记录包含12个数字,所以它可能看起来像:

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++过载运算符流示例”。

我建议获得斯科特·梅耶的C++有效系列,其中包含了解释和超载operator>>operator<<的例子。

+0

嗨托马斯,原谅我,但我不是一个专业编码员,只是想学习。你的解释根本不在我的脑海中。 – Gunn

+0

@Gunn:在我的回答中看到我的**编辑1 **。 –