C++ 如何读取每25行重复一次特定结构的ASCII文件?

C++ 如何读取每25行重复一次特定结构的ASCII文件?,c++,ascii,C++,Ascii,我有一个ASCII数据文件,在25行中有不同的变量,重复。我想在每个结构上做一个循环,对于每个结构,元素将被添加到数组中。例如,结构中的第一个数字或数字将是事件编号,从1、2、3。。。等等,第二个数字将是该事件的时间(linux时间格式)。下面是重复文件的示例 1 1481492919 298362 1 936 642 618 1346 2648 0 103 1651 69 76 7 0

我有一个ASCII数据文件,在25行中有不同的变量,重复。我想在每个结构上做一个循环,对于每个结构,元素将被添加到数组中。例如,结构中的第一个数字或数字将是事件编号,从1、2、3。。。等等,第二个数字将是该事件的时间(linux时间格式)。下面是重复文件的示例

   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

一种典型的方法是用一种结构对每一行文本进行建模。这可以称为记录

看起来您的文件格式有不同类型的记录,因此需要不同的结构

在每个结构中,重载
操作符>>
以从格式化流中读取成员。这样可以简化编程:

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);
}
您的输入循环可能如下所示:

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;
}
如果需要,可以将行封装到一个类中:

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;
}
您的文件将是一个包含
数据\u项的容器
,因此让我们以这种方式阅读它:

std::vector<Data_Item> database;
Data_item d;
while (data_file >> d)
{
    data_file >> d;
    database.push_back(d);
}
std::向量数据库;
数据项d;
而(数据文件>>d)
{
数据文件>>d;
数据库。推回(d);
}
有关重载输入运算符的更多信息,请在internet上搜索“c++重载运算符流示例”


推荐Scott Meyer有效C++系列,包含了“超代码< >代码>运算符> <代码> >代码>操作程序的说明和示例,请显示您已经做过的操作。类模板有必要的工具。@ SergYES是的,我能用IFSF F(AFILE)读取和打印终端中的ASCII;弦线;而(!f.eof()){getline(f,line);if(line[0]='e'| | line[0]='f'| | line[1]='f'| | line[1]='n'| | line[2]='f')cout@Gunn请在您的帖子中包含相关代码。更喜欢在程序中以文本形式发布代码和数据。图形取决于阅读器显示器的分辨率和大小。在手机或设备上查看可能不可行。嗨,托马斯,请原谅我,但我不是一名专业程序员,只是想学习。您的解释根本不可行进入我的头脑。@Gunn:在我的回答中看到我的编辑1。

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