c++ - "Ends_with"读取输入文件时函数不工作

标签 c++ boost ends-with

我正在尝试创建一个使用 boost 库读取如下输入文件的 C++ 代码,

    1             12       13        0        0      1      0      INLE
    .
    .
    .

在这种情况下,如果右侧最后一列指定的条件是 INLE,我必须执行操作。 我有以下代码,

#include <iostream>
#include <fstream>
#include <string>
#include <boost/algorithm/string/predicate.hpp>


int main(int argc, const char * argv[])
{
    std::string line;
    const std::string B_condition = "INLE";
    std::ifstream myfile ("ramp.bnd");
    if (myfile.is_open())
    {
        while ( getline (myfile,line) )
        {
            if (boost::algorithm::ends_with(line,B_condition)==true)
            {
                std::cout << "Its True! \n"; // just for testing
                //add complete code
            }
        }
        myfile.close();
    }

    else std::cout << "Unable to open file";

    return 0;
}

编译时没有问题,但是当我运行时,它什么也没有显示。

另一方面,如果我将我的 bool 条件修改为 false,它会打印“Its true!”我的输入文件的行数。

我做错了什么? 谢谢!!

最佳答案

我只能假设:

  • 您的文件末尾包含空格(使用修剪)
  • 您的文件有 Windows 行结束符 (CRLF)但是您正在将其作为 UNIX 文本文件读取,这意味着这些行将包含尾随的“\r”( CR)(在各种文本编辑器/寻呼机中通常显示为 ^M)。

所以,要么

  • 修复行尾
  • 比较前去掉行中的空格
  • 两者

最好:使用“适当的”解析器来完成这项工作。

更新 使用 Boost Spirit 添加了一种快速但肮脏的方法:参见 Live On Coliru

int main()
{
    std::ifstream myfile("ramp.bnd");
    myfile.unsetf(std::ios::skipws);

    boost::spirit::istream_iterator f(myfile), l;

    using namespace qi;
    bool ok = phrase_parse(f, l,
            (repeat(7) [ int_ ] >> as_string[lexeme[+(char_ - eol)]])
                [ phx::bind(process_line, _1, _2) ]
            % eol, // supports CRLF and LF
            blank);

    if (!ok)
        std::cerr << "Parse errors\n";
    if (f!=l)
        std::cerr << "Remaing input: '" << std::string(f,l) << "'\n";
}

如您所见,它验证了整行,假设(现在)列是 7 个整数值和一个字符串(例如 "INLE")。现在,实际工作要简单得多,可以在一个单独的函数中实现:

void process_line(std::vector<int> const& values, std::string const& kind)
{
    if (kind == "INLE")
    {
        std::cout << "Column 1: " << values[0] << "\n";
    }
}

实际的处理函数不必干预修剪、行结束,甚至解析详细信息列:)

完整代码供引用

#include <iostream>
#include <fstream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>

namespace qi  = boost::spirit::qi;
namespace phx = boost::phoenix;

static const std::string B_condition = "INLE";

void process_line(std::vector<int> const& values, std::string const& kind)
{
    if (kind == "INLE")
    {
        std::cout << "Column 1: " << values[0] << "\n";
    }
}

int main()
{
    std::ifstream myfile("ramp.bnd");
    myfile.unsetf(std::ios::skipws);

    boost::spirit::istream_iterator f(myfile), l;

    using namespace qi;
    bool ok = phrase_parse(f, l,
            (repeat(7) [ int_ ] >> as_string[lexeme[+(char_ - eol)]])
                [ phx::bind(process_line, _1, _2) ]
            % eol, // supports CRLF and LF
            blank);

    if (!ok)
        std::cerr << "Parse errors\n";
    if (f!=l)
        std::cerr << "Remaing input: '" << std::string(f,l) << "'\n";
}

关于c++ - "Ends_with"读取输入文件时函数不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22500737/

相关文章:

c++ - QTcpSocket 不加载 ssl

javascript - Qt Qml Javascript - 在哪里使用哪个?

c++ - 从偏移量将数组的内容写入 C++ vector

boost - 如何在 Visual Studio 2017 中为 CMake 指定 Boost 位置

c++ - 在 std::map 中存储 boost::bind 函数

ruby - 如何查看字符串是否以多个字符之一结尾

c++ - 进一步的右值引用和临时对象

c++ - boost::allocate_unique 产生非默认可构造和不可移动可分配的 unique_ptrs

javascript - 如何检查 string.endsWith 并忽略空格

python - 类型错误 : endswith first arg must be str or a tuple of str, 不是 boolean 值