【问题标题】:Read input file with backslash (\) delimiter c++用反斜杠(\)分隔符c ++读取输入文件
【发布时间】:2013-02-22 17:03:59
【问题描述】:

我的输入文件是这样的:

C:\Users\DeadCoder\AppData\Local\CoCreate

我正在制作一棵树,我需要在使用 \ 分隔符从输入文件读取时抽象目录名称。和上面的例子一样,我需要分别抽象出c:、users、DeadCoder、Appdata ....希望大家理解问题。 现在让我们看看我们得到的选项。

1- istringstream 适用于 whitespace 但不适用于 \

2- strtok() 作用于 char。所以我必须将我的字符串更改为 char,我真的不想这样做。

3- Boost Tokenizer()这个看起来很有趣,我过去对它并不熟悉,只是我刚刚在谷歌上搜索了一下。我复制了代码,是这样的:

#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
using namespace boost;

int main(){

    string tempStr;
    ifstream fin;
    fin.open("input.txt");
    int i=0;

    while (!fin.eof()){
        getline(fin,tempStr);
        char_separator<char> sep("\"); // error: missing terminating " character
        tokenizer<char_separator<char>> tokens(tempStr, sep);
        for (const auto& t : tokens) {
            cout << t << "." << endl;
        }
}

现在这给出了"error: boost/foreach.hpp: No such file or directory" 的错误 有人可以在这里帮助我。还有其他better way 可以用\ delimiter 读取输入文件吗?请不要使用像class tokenizer() 这样的大量代码,因为我还在学习c++。

编辑:我没有安装 boost 库,因此我遇到了这个错误。如果有人可以在不安装第三个库的情况下解释tokenize 字符串的更好方法,那将是非常受欢迎的。

最好的; 死编码器。

【问题讨论】:

  • 你安装Boost库了吗?
  • 不。不是默认在那里吗?
  • 这是一个第三方库。从boost.org下载它
  • 好吧好吧。有没有其他方法可以完成这项任务,我的意思是我不必添加任何第三个库。
  • 是的。从文件中读取该行。根据分隔符对字符串进行标记。将标记存储在 std::vector<:string> 中。有关参考,请参阅此线程。 stackoverflow.com/questions/53849/…

标签: c++ stringtokenizer boost-tokenizer


【解决方案1】:

在 C++(和其他基于 C 的语言)中,字符串或字符文字中的 \ 字符是 转义 字符。这意味着它转义文字中的下一个字符。例如,您可以在字符串中包含"。要在字符串文字中包含\,您需要通过其中两个来转义反斜杠:"\\"

您可以阅读有关 C++ 中有效转义序列的更多信息,例如in this reference.


至于 Boost 的问题,你需要告诉编译器你安装它的位置。这是在 IDE 的项目属性中完成的。


如果您想在 的情况下使用第三方库(例如 Boost)进行标记,有几种方法。一种方法是使用std::istringstreamstd::getline。另一个使用标准string 类的findsubstr 函数。

【讨论】:

  • 好的,但我真的需要知道为什么我必须使用两个 \\ 。因为你在上面的陈述中对我没有意义。
  • @DeadCoder 你知道如何在字符串中编写文字换行符吗?您使用"\n" 对吗?这与使用"\\" 表示单个反斜杠相同。字符或字符串 literal 中的反斜杠告诉编译器下一个字符是表示其他含义的特殊字符。
  • @DeadCoder 你必须使用两个 \\ 因为这是定义语言的方式。
  • @JamesKanze 和 Joachmin ...非常感谢。
【解决方案2】:

这里的任何类型的通用标记器都将是矫枉过正。只需使用 std::find( s.begin(), s.end(), '\\' ) 查找每个分隔符, 和std::string的两个迭代器构造函数放入 一个单独的字符串。 (您的编译器将第一个 \ 视为 转义字符。)类似:

std::vector<std::string> fields;
std::string::const_iterator end = s.end();
std::string::const_iterator current = s.begin();
std::string::const_iterator next
        = std::find( current, end, '\\' ):
while ( next != end ) {
    fields.push_back( std::string( current, next ) );
    current = next + 1;
    next = std::find( current, end, '\\' );
}
fields.push_back( std::string( current, next ) );

应该可以解决问题。

【讨论】:

    【解决方案3】:
    char_separator<char> sep("\") 
                              ^^^ You need to escape the \ . use "\\" 
    

    \ 用于指示转义序列。但是to escape that escape, you need other escape

    使用这个:char_separator&lt;char&gt; sep("\\")

    要安装 boost 库:Install Boost

    其他选择:

    getline(fin,tempStr);
    char *cstr=new char[tempStr.length()+1];
    strcpy(cstr,tempStr.c_str())
    
    //... Now you can use strtok() on cstr
    

    【讨论】:

      猜你喜欢
      • 2020-05-06
      • 1970-01-01
      • 2011-09-16
      • 2018-08-07
      • 1970-01-01
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多