首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正在从boost::tuples::tuple检索字符串

正在从boost::tuples::tuple检索字符串
EN

Stack Overflow用户
提问于 2019-08-16 14:31:46
回答 1查看 18关注 0票数 0

https://www.boost.org/doc/libs/1_67_0/libs/tuple/doc/html/tuple_users_guide.html上阅读了这篇文章后,下面的说明对我来说是一个问题。请注意,使用std::string或C样式字符串元素提取元组通常不起作用,因为流式元组表示可能不是明确可解析的。我应该使用什么类型来明确地将流中的字符串解析为元组?

从元组中检索std::string时,字符串由空格分隔。这不是我们想要的!设置分隔符(例如,数字符号(#))无济于事。

代码语言:javascript
复制
// typedef tuple
typedef std::string td_current_gmt, td_remote_endpoint, 
    td_request, td_response, td_elapsed_time;
typedef boost::tuples::tuple<td_current_gmt, td_remote_endpoint, 
    td_request, td_response, td_elapsed_time> tuple_logging;
// store in tuple
tuple_logging tl{ current_gmt, remote_endpoint, 
    request, response, elapsed_time };
// write tuple to file
tl = boost::tuples::make_tuple(current_gmt, remote_endpoint,
    request, response, elapsed_time);
boost::filesystem::path p = { "logging" };
boost::filesystem::ofstream ofs{ p };
ofs << /*boost::tuples::set_delimiter('#') <<*/ tl;
ofs.close();
// read tuple from file
tuple_logging tlin{ current_gmt, remote_endpoint,
    request, response, elapsed_time };
boost::filesystem::ifstream ifs{ p };
//ifs >> boost::tuples::set_delimiter('#');
ifs >> tlin;

输出是(Fri,Aug 16 201906:28:05),但实际上必须是(Fri,Aug 16 201906:28:05 GMT 192.168.178.14:52832 TRACE /HTTP/1.1HTTP/1.1200 OK 8.936800)

EN

回答 1

Stack Overflow用户

发布于 2019-08-18 14:37:03

这是代码。

代码语言:javascript
复制
void parse_logfile(
boost::filesystem::ifstream& ifs,
const boost::filesystem::path& p,
std::vector<tuple_logging>& vector_with_tuples

){类型定义函数std::string one_line_from_logging;

代码语言:javascript
复制
one_line_from_logging str;
tuple_logging tlin;

// clear vector
vector_with_tuples.clear();

// open log file for reading
ifs.open(p);

// read one line from log file, until eof
while (std::getline(ifs, str))
{
    size_t sBegin = 0, sEnd = 0;
    // 1
    // first character on a line is '(',
    // start at sBegin = 1
    sBegin = sEnd + 1;
    sEnd = str.find('#', sBegin);
    std::string current_gmt_ = str.substr(sBegin, sEnd - sBegin);
    // 2
    sBegin = sEnd + 1;
    sEnd = str.find('#', sBegin);
    std::string remote_endpoint_ = str.substr(sBegin, sEnd - sBegin);
    // 3
    sBegin = sEnd + 1;
    sEnd = str.find('#', sBegin);
    std::string request_ = str.substr(sBegin, sEnd - sBegin);
    // 4
    sBegin = sEnd + 1;
    sEnd = str.find('#', sBegin);
    std::string response_ = str.substr(sBegin, sEnd - sBegin);
    // 5
    sBegin = sEnd + 1;
    // last character on a line is ')'
    sEnd = str.find(')', sBegin);
    std::string elapsed_time_ = str.substr(sBegin, sEnd - sBegin);

    // create tuple from parsed log data out of file
    tlin = boost::tuples::make_tuple(
        current_gmt_,
        remote_endpoint_,
        request_,
        response_,
        elapsed_time_
    );

    // set tuple into vector
    vector_with_tuples.push_back(tlin);
}

// close log file
ifs.close();

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57519875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档