仅将某些数据行从 .arff 文件读取到结构C++中
Only reading certain lines of data from .arff file into struct C++
我有一个看起来像这样的.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++;
}
}
下面是一个示例,可帮助您入门:
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
循环的顶部,从而忽略该行。
相关文章:
- 使用新行和不使用新行读取文件
- 读取文件并输入到矢量中
- 读取文件的最后一行并输入到链接列表时出错
- 为什么在读取文件大小时文件IO速度会发生变化
- 读取文件时运行时的未知行为
- 如何逐行读取文件,每行中的内容都用空格分隔并将其写入新文件中
- 读取文件中所有可能的十六进制 16 字节序列并打印每个序列
- C++ 读取文件读取文件不正确
- 读取文件并将其存储在unordered_map中时出现问题
- 读取文件时无法使用 OpenMP 获得加速
- 使用istringstearm和get行缓慢读取文件
- 如何继续读取不同功能中的文件,而不是从头开始再次读取文件?
- 读取文件时引发异常
- 从标准输入读取文件后读取用户输入
- 在读取文件后重置句柄
- 如何在C++编译时读取文件?
- std::ifstream 在读取文件中最后一项时设置 eofbit,但仅在读取数值类型时发生
- 读取文件在第二次调用时返回INVALID_HANDLE
- 通过指针读取文件
- 逐行读取文件,并将数据插入变量和数组中