比较和拆分文本文件中的字符串
Compare and split strings in text file
我试图用 C++ 打开一个文本文件并逐行读取它直到找到特定的字符串,但是数据也被分成不同的部分...
文本文件如下所示:
entry_one:true,true,false;
entry_two:true,false,false;
entry_three:false,false,false;
entry_four:true,false,true;
我基本上需要将文本文件读入内存,遍历每一行直到找到我要查找的特定 "entry_...",然后我需要将布尔值数据拆分为变量。
到目前为止我是这样想的:
ifstream stream;
string line;
stream.open(filepath);
if (stream)
{
size_t sizepos;
while (stream.good()){
getline(stream, line);
}
}
以上代码至少会将文本文件打开到内存中以便读取,然后可以使用getline读取当前行...
但是我如何检查它是否是我需要的正确条目,然后将 true/false 拆分为变量?
例如在文本文件中找到第二个条目,并将每个 true/false 放入以下变量
bool bEntryTwo1 = false;
bool bEntryTwo2 = false;
bool bEntryTwo3 = false;
我很困惑和困惑,有谁知道他们在做什么来帮助我解决这个问题?谢谢! :)
我推荐建模文本行作为记录,然后读取每条记录:
struct Record
{
std::string name;
bool entry1;
bool entry2;
bool entry3;
};
接下来是在operator>>
中添加读入数据成员:
struct Record
{
//...
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
std::string name;
std::getline(input, r.name, ',');
char comma;
input >> r.entry1 >> comma >> r.entry2 >> comma >> r.entry3;
return input;
}
然后您将从文件中读取每条记录并只处理您要查找的记录:
Record r;
while (my_data_file >> r)
{
if (r.name = Name_To_Find)
{
Process_Record(r);
break;
}
}
阅读记录的一个好处是,当您找到名字时,您拥有完整的信息。
另一种方法是将所有记录读入内存(例如std::vector
),然后搜索内存找到它。
有关详细信息,请在互联网上搜索 "Whosebug c++ read file CSV"。
对于布尔值,我将使用 std::vector<bool>
而不是创建单独的布尔值。因此,这是我解决您问题的方法。
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
int main()
{
std::ifstream file("data.txt");
std::string entry("entry_four");
std::string line;
std::vector<bool> boolVals;
std::string vars;
while ( std::getline(file, line) ){
int id(line.find(':'));
std::string temp(line.substr(0,id));
if ( entry == temp ){
vars = line.substr(id+1);
std::string temp1;
for ( int i(0); i < vars.size(); ++i ){
if ( (vars[i] != ',') && (vars[i] != ';') ){
temp1 += vars[i];
}else{
if ( temp1 == "true" )
boolVals.push_back(true);
else
boolVals.push_back(false);
temp1.clear();
}
}
}
}
std::cout << vars << std::endl;
for ( int i(0); i < boolVals.size(); ++i ){
std::cout << boolVals[i] << " ";
}
std::cout << std::endl;
return 0;
}
对于这个条目entry_four:true,false,true;
,我们有这个输出
true,false,true;
1 0 1
注意:为了简单起见,我忽略了代码中的一些细节。例如,您需要仔细检查文件是否打开成功。我会把额外的预防措施留给你。
我试图用 C++ 打开一个文本文件并逐行读取它直到找到特定的字符串,但是数据也被分成不同的部分...
文本文件如下所示:
entry_one:true,true,false;
entry_two:true,false,false;
entry_three:false,false,false;
entry_four:true,false,true;
我基本上需要将文本文件读入内存,遍历每一行直到找到我要查找的特定 "entry_...",然后我需要将布尔值数据拆分为变量。
到目前为止我是这样想的:
ifstream stream;
string line;
stream.open(filepath);
if (stream)
{
size_t sizepos;
while (stream.good()){
getline(stream, line);
}
}
以上代码至少会将文本文件打开到内存中以便读取,然后可以使用getline读取当前行...
但是我如何检查它是否是我需要的正确条目,然后将 true/false 拆分为变量?
例如在文本文件中找到第二个条目,并将每个 true/false 放入以下变量
bool bEntryTwo1 = false;
bool bEntryTwo2 = false;
bool bEntryTwo3 = false;
我很困惑和困惑,有谁知道他们在做什么来帮助我解决这个问题?谢谢! :)
我推荐建模文本行作为记录,然后读取每条记录:
struct Record
{
std::string name;
bool entry1;
bool entry2;
bool entry3;
};
接下来是在operator>>
中添加读入数据成员:
struct Record
{
//...
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
std::string name;
std::getline(input, r.name, ',');
char comma;
input >> r.entry1 >> comma >> r.entry2 >> comma >> r.entry3;
return input;
}
然后您将从文件中读取每条记录并只处理您要查找的记录:
Record r;
while (my_data_file >> r)
{
if (r.name = Name_To_Find)
{
Process_Record(r);
break;
}
}
阅读记录的一个好处是,当您找到名字时,您拥有完整的信息。
另一种方法是将所有记录读入内存(例如std::vector
),然后搜索内存找到它。
有关详细信息,请在互联网上搜索 "Whosebug c++ read file CSV"。
对于布尔值,我将使用 std::vector<bool>
而不是创建单独的布尔值。因此,这是我解决您问题的方法。
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
int main()
{
std::ifstream file("data.txt");
std::string entry("entry_four");
std::string line;
std::vector<bool> boolVals;
std::string vars;
while ( std::getline(file, line) ){
int id(line.find(':'));
std::string temp(line.substr(0,id));
if ( entry == temp ){
vars = line.substr(id+1);
std::string temp1;
for ( int i(0); i < vars.size(); ++i ){
if ( (vars[i] != ',') && (vars[i] != ';') ){
temp1 += vars[i];
}else{
if ( temp1 == "true" )
boolVals.push_back(true);
else
boolVals.push_back(false);
temp1.clear();
}
}
}
}
std::cout << vars << std::endl;
for ( int i(0); i < boolVals.size(); ++i ){
std::cout << boolVals[i] << " ";
}
std::cout << std::endl;
return 0;
}
对于这个条目entry_four:true,false,true;
,我们有这个输出
true,false,true;
1 0 1
注意:为了简单起见,我忽略了代码中的一些细节。例如,您需要仔细检查文件是否打开成功。我会把额外的预防措施留给你。