文件ponter维护错误请指导 [英] File ponter maintaince error please guide
问题描述
大家好,
我有一个这样的文件
我是学生xyz in大学|美国|在第一年。
我是大学的学生|美国|在第一年。
我是大学生|意大利|在第一年。
我必须解析文件并从文件中提取大学名称以及xyz,abc,asd
所以我写了以下代码
// #include< stdio.h>
#include < fstream >
#include < 字符串 >
# include < iostream >
bool P arseLine( const std :: string& line,std :: string& key,std :: string& value );
class 演示
{
public :
typedef struct _Data
{
std :: string university;
std :: string name;
}数据;
数据数据;
};
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( 学生))
{
obj.data.name = value 跨度>;
}
else if ( 0 == key.compare( university))
{
obj.data.content = value ;
}
else
{
std :: cerr<< 未知密钥:<< key<<的std :: ENDL;
}
}
}
返回 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;
}
两个问题
在提取大学价值时我会得到像美国这样的东西|在第一年但我只想要美国和名字逻辑不工作
请指导
1)不确定为什么在ParseLine中检查;,但是你需要检查第二个|,例如
size_t pos = line.find( |);
if (pos!= std :: string :: npos)
{
size_t pos1 = line.find( |,pos + 1 );
value = line.substr(pos + 1 ,pos1 - pos - 1 );
}
2)对学生姓名最简单的解决方案是产生与大学价值相似的逻辑,例如
size_t pos = line.find( student);
if (pos!= std :: string :: npos)
{
size_t pos1 = line.find( ,pos + 8 跨度>);
key = line.substr(pos + 8 ,pos1 - pos - 8 );
}
以下是主函数中的以下内容:
< pre lang =c ++> if (ParseLine(line,key, value ))
{
obj.data.name = key;
obj.data.content = value ;
}
(你应该在if语句中创建一个函数来验证密钥和/或值,以确保它们是可接受的值)
Hi All,
I have a file like this
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.
I have to parse the file and extract only the university name from the file and also the xyz,abc,asd
so i have written the following code
//#include <stdio.h>
#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;
}
Two problems
When extracting the university value i get every thing like "USA|" in first year" but i want only USA and name logic dosen't work
Please guide
1) Not sure why you check for ";" in ParseLine, but you would need to check for the 2nd "|", e.g.
size_t pos = line.find("|"); if (pos != std::string::npos) { size_t pos1 = line.find("|", pos + 1); value = line.substr(pos + 1, pos1 - pos - 1); }
2) The easiest solution to the student name is to produce similar logic as the university value, e.g.
size_t pos = line.find("student "); if (pos != std::string::npos) { size_t pos1 = line.find(" ", pos + 8); key = line.substr(pos + 8, pos1 - pos - 8); }
Following this would then simply have the following in the main function:
if (ParseLine(line, key, value)) { obj.data.name = key; obj.data.content = value; }
(You should probably create a function inside the if statement to validate the key and/or value to ensure they are acceptable values)
这篇关于文件ponter维护错误请指导的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!