C++;在两个字符串之间提取数据 我正在寻找一个C++代码,它可以从两个字符串之间的文件示例.txt中提取一些特定的内容,而忽略其他内容。例如,example.txt文件有以下几行 xyz abc ['Content','en']], <html>hi this is a line <br></html> ',true], suzi 20 xyz abc ['Content','en']], 嗨,这是一条线 “,对], 苏西20
我想提取['Content','en']],和',true]之间的代码,这意味着C++;在两个字符串之间提取数据 我正在寻找一个C++代码,它可以从两个字符串之间的文件示例.txt中提取一些特定的内容,而忽略其他内容。例如,example.txt文件有以下几行 xyz abc ['Content','en']], <html>hi this is a line <br></html> ',true], suzi 20 xyz abc ['Content','en']], 嗨,这是一条线 “,对], 苏西20,c++,extract,fstream,ifstream,ofstream,C++,Extract,Fstream,Ifstream,Ofstream,我想提取['Content','en']],和',true]之间的代码,这意味着 <html>hi this is a line <br></html> hi这是一条线 请注意,我不擅长编程和使用dev++编译器。最简单的方法是将文件读入字符串,然后提取内容: #include <string> #include <sstream> std::string extract(std::string const& tag_be
<html>hi this is a line <br></html>
hi这是一条线
请注意,我不擅长编程和使用dev++编译器。最简单的方法是将文件读入字符串,然后提取内容:
#include <string>
#include <sstream>
std::string extract(std::string const& tag_begin, std::string const& tag_end, std::istream& input)
{
// file stream -> in memory string
std::string filedata((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());
// find content start and stop
auto content_start = filedata.find(tag_begin);
if (content_start == std::string::npos) {
return ""; // error handling
}
content_start += tag_begin.size();
auto content_end = filedata.find(tag_end, content_start);
auto content_size = content_end - content_start;
// extract (copy) content to other string
auto content = filedata.substr(content_start, content_size);
return content;
}
#包括
#包括
std::string extract(std::string const&tag_begin,std::string const&tag_end,std::istream&input)
{
//文件流->内存中字符串
std::string文件数据((std::istreambuf_迭代器(输入)),std::istreambuf_迭代器();
//查找内容开始和停止
自动内容\u开始=filedata.find(标记\u开始);
if(content_start==std::string::npos){
返回“”;//错误处理
}
content_start+=标记_begin.size();
自动内容结束=filedata.find(标记结束,内容开始);
自动内容大小=内容结束-内容开始;
//将内容提取(复制)到其他字符串
自动内容=filedata.substr(内容开始,内容大小);
返回内容;
}
然后,您需要根据自己的需要调整此通用解决方案。1。不要使用dev-c++。2.阅读
regex
寻找解决方案,没有regex能做到吗?如果是,请提供一份code@AhmedMehtab这不是一个代码编写服务;您应该发布您尝试过的内容,并突出显示您遇到问题的特定点。您可以直接从两个迭代器创建内容
字符串,不需要content\u size
@bolov我必须从content\u start
和content\u stop
中获得开始和结束迭代器,这相对来说同样令人讨厌。我认为content\u start
和content\u stop
都是迭代器。我的错误。