【问题标题】:Only reading certain lines of data from .arff file into struct C++仅将 .arff 文件中的某些数据行读取到 struct C++
【发布时间】:2018-03-08 22:02:19
【问题描述】:

我有一个看起来像这样的 .arff 文件

% Title: Database for fitting contact lenses
% 
% Number of Instances: 24
% 
% Number of Attributes: 4 (all nominal)
% 
% Attribute Information -- 3 Classes:
%   1 : the patient should be fitted with hard contact lenses,
%   2 : the patient should be fitted with soft contact lenses,
%   3 : the patient should not be fitted with contact lenses.
%  
% Class Distribution:
%    1. hard contact lenses: 4
%    2. soft contact lenses: 5
%    3. no contact lenses: 15

@relation contact-lenses

@attribute age          {young, pre-presbyopic, presbyopic}
@attribute spectacle-prescrip   {myope, hypermetrope}
@attribute astigmatism      {no, yes}
@attribute tear-prod-rate   {reduced, normal}
@attribute contact-lenses   {soft, hard, none}

@data
%
% 24 instances
%
young,myope,no,reduced,none
young,myope,no,normal,soft
young,myope,yes,reduced,none
young,myope,yes,normal,hard
young,hypermetrope,no,reduced,none
young,hypermetrope,no,normal,soft
young,hypermetrope,yes,reduced,none
young,hypermetrope,yes,normal,hard
pre-presbyopic,myope,no,reduced,none
pre-presbyopic,myope,no,normal,soft
pre-presbyopic,myope,yes,reduced,none
pre-presbyopic,myope,yes,normal,hard
pre-presbyopic,hypermetrope,no,reduced,none
pre-presbyopic,hypermetrope,no,normal,soft
pre-presbyopic,hypermetrope,yes,reduced,none
pre-presbyopic,hypermetrope,yes,normal,none
presbyopic,myope,no,reduced,none
presbyopic,myope,no,normal,none
presbyopic,myope,yes,reduced,none
presbyopic,myope,yes,normal,hard
presbyopic,hypermetrope,no,reduced,none
presbyopic,hypermetrope,no,normal,soft
presbyopic,hypermetrope,yes,reduced,none
presbyopic,hypermetrope,yes,normal,none

而我只想读取上面有数据的行,例如

young,myope,no,reduced,none
young,myope,no,normal,soft
young,myope,yes,reduced,none

进入一个结构。该结构对 5 条数据中的每一条都有一个字符串成员。如何编写循环或循环组合来跳过我不关心的行,并将我要查找的数据读入结构?

编辑:我只想读取不以 %、@ 或空行开头的行。我不明白为什么这不起作用。

while(inFile.good())
{
    getline(inFile,line);

    if((line[0] == '%') || (line[0] == '@') || (line[0] == ' '))
    {
        cout << "This line we dont care about" << endl;
    }

    else
    {
        cout << "Made it into the else" << endl;
        getline(inFile,line,',');
        data[count].age = line;
        cout << "data[0] = " << data[count].age << endl;
        getline(inFile,line,',');
        data[count].prescription = line;
        cout << "data[0] = " << data[count].prescription << endl;
        getline(inFile,line,',');
        data[count].astig = line;
        getline(inFile,line,',');
        data[count].tearProduction = line;
        getline(inFile,line);
        data[count].contacts = line;

        count++;
    }

}

【问题讨论】:

  • 我建议研究“lexing”和“parsing”。您可能需要数据表或文件格式要求,以找出“数据”行的开始位置。最坏的情况,读一行,如果不是你想要的,忽略它并读下一行。
  • 基本上我想读取任何不以@、% 或空行开头的行。有没有办法检查?
  • 是的,将使用std::getline 的行读入std::string 并检查第一个字符。
  • @ThomasMatthews 我正在尝试,但它似乎不起作用。我已经用我正在使用的代码编辑了我的问题,请你看一下吗?
  • 您的代码的一个问题是,如果该行为空,则 line[0] 将未定义。您需要先检查长度。

标签: c++ struct file-io


【解决方案1】:

这里有一个示例可以帮助您入门:

std::string text_line;
while (std::getline(my_data_file, text_line)
{
  // Check the line length first.  Empty lines are ignored.
  if (text_line.length() == 0)
  {
    continue;
  }

  // Test lines for rejection by reading the first character.
  const char c = text_line[0];
  if ((c == '@') || (c == '%') || (c == ' '))
  {
    continue;
  }
  // Add code to parse the data lines
}

continue 将导致执行转到while 循环的顶部,从而忽略该行。

【讨论】:

  • 我用这段代码给它一个测试运行,它工作得几乎完美。出于某种原因,在数据开始之前,它会打印 3 个空白行。你碰巧知道为什么吗?编辑:它还切断了该行的第一个字母,因此它会打印出 oung 而不是 young
  • 上面发布的代码都没有修改text_line。你的输出取决于你把cout放在哪里。第一个字符可以是制表符或其他空格以及非打印控制字符。
  • 这似乎是一个奇怪的打印错误。你知道为什么空白行还在打印吗?或者我如何解析数据行以将每个字符串(用逗号分隔)放入一个结构中?
  • 要解析成结构体,在网上搜索“StackOverflow c++ 读取文件结构体逗号分隔”。
猜你喜欢
  • 1970-01-01
  • 2020-07-16
  • 2019-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
相关资源
最近更新 更多