我有這樣文件解析代碼需要注意
I am a student xyz in university"|USA|" in first year.
I am a student abc in university"|America|" in first year.
I am a student asd in university"|Italy|" in first year.
我要解析的文件,從文件,也是XYZ只提取大學名abc,ASD 文件,所以我寫的下面的代碼
#include <fstream>
#include <string>
#include <iostream>
bool ParseLine(const std::string& line, std::string& key, std::string& value);
class Demo
{
public:
typedef struct _Data
{
std::string university;
std::string name;
} Data;
Data data;
};
int main()
{
std::ifstream ifs("Ca.txt");
std::string line;
std::string key;
std::string value;
Demo obj;
while(!ifs.eof())
{
std::getline(ifs, line);
if (ParseLine(line, key, value))
{
if (0 == key.compare("student"))
{
obj.data.name = value;
}
else if (0 == key.compare("university"))
{
obj.data.content = value;
}
else
{
std::cerr<<"Unknow key:" << key << std::endl;
}
}
}
return 0;
}
bool ParseLine(const std::string& line, std::string& key, std::string& value)
{
bool flag = false;
if (line.find("//") != 0)
{
size_t pos = line.find("|");
if (pos != std::string::npos)
{
key = line.substr(0, pos);
size_t pos1 = line.find(";");
value = line.substr(pos + 1, pos1);
flag = true;
}
}
return flag;
}
兩個問題 在提取大學價值我得到的每一件事情,如「美國|」在第一年,」但我想只有美國和名稱邏輯這麼想的工作
請指導
什麼是'obj.data.content'? – 0x499602D2
@ 0x499602D2它應該是obj.data.university對不起我的錯誤 – user3340847
@ 0x499602D2任何解決方案? – user3340847