首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >只将某些行数据从.arff文件读取到struct C++中

只将某些行数据从.arff文件读取到struct C++中
EN

Stack Overflow用户
提问于 2018-03-08 22:02:19
回答 1查看 329关注 0票数 0

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

代码语言:javascript
复制
% 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

我只想读那些有数据的行,比如

代码语言:javascript
复制
young,myope,no,reduced,none
young,myope,no,normal,soft
young,myope,yes,reduced,none

变成了一个结构。该结构为5个数据段中的每一个都有一个字符串成员。如何编写循环或循环组合,以跳过我不关心的行,并将正在查找的数据读入结构中?

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

代码语言:javascript
复制
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++;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-08 22:54:40

下面是一个让您开始学习的示例:

代码语言:javascript
复制
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循环的顶部,从而忽略行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49183212

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档