c++ - strtok 或 std::istringstream

标签 c++ strtok istringstream

我有以下代码,它使用从 txt 文件接收输入的 strtok。 txt文件中的输入是:

age, (4, years, 5, months)
age, (8, years, 7, months)
age, (4, years, 5, months)

我的代码如下:

char * point;
ifstream file;
file.open(file.c_str());

if(file.is_open())
{
    while(file.good())
    {
        getline(file, take);
        point = strtok(&take[0], ", ()");
    }
}

除了缺少第 2 个年龄段和第 3 个年龄段的输出外,一切正常。谁能告诉我他们为什么失踪?

我也试过 istringstream 但每当我输入我的文件名时,程序就会崩溃。

char * point;
char take[256];
ifstream file;
file.open(file.c_str());

if(file.is_open())
{
    while(file.good())
    {
        cin.getline(take, 256);
        point =strtok(take,", ()");
    }
}

最佳答案

就个人而言,我会使用 std::istringstream 但我会以不同的方式使用它(...而且,是的,我知道我可以使用 sscanf()以及代码会更短,但我不喜欢类型不安全的接口(interface))!我会玩操纵器的把戏:

#include <iostream>
#include <sstream>
#include <string>

template <char C>
std::istream& skip(std::istream& in)
{
    if ((in >> std::ws).peek() != std::char_traits<char>::to_int_type(C)) {
        in.setstate(std::ios_base::failbit);
    }
    return in.ignore();
}

std::istream& (*const comma)(std::istream&) = &skip<','>;
std::istream& (*const open)(std::istream&) = &skip<'('>;
std::istream& (*const close)(std::istream&) = &skip<')'>;

struct token
{
    token(std::string const& value): value_(value) {}
    std::string::const_iterator begin() const { return this->value_.begin(); }
    std::string::const_iterator end() const   { return this->value_.end(); }
    std::string value_;
};

std::istream& operator>> (std::istream& in, token const& t)
{
    std::istreambuf_iterator<char> it(in >> std::ws), end;
    for (std::string::const_iterator sit(t.begin()), send(t.end());
         it != end && sit != send; ++it, ++sit) {
        if (*it != *sit) {
            in.setstate(std::ios_base::failbit);
            break;
        }
    }
    return in;
}

int main()
{
    std::istringstream input("age, (4, years, 5, months)\n"
                             "age , ( 8 , years , 7, months )\n"
                             "age, (4, year, 5, months)\n"
                             "age, (4, years 5, months)\n"
                             "age (4, years, 5, months)\n"
                             "age, 4, years, 5, months)\n"
                             "age, (4, years, 5, months)\n");
    std::string dummy;
    int         year, month;
    for (std::string line; std::getline(input, line); ) {
        std::istringstream lin(line);
        if (lin >> token("age") >> comma
            >> open
            >> year >> comma >> token("years") >> comma
            >> month >> comma >> token("months") >> close) {
            std::cout << "year=" << year << " month=" << month << "\n";
        }
    }
}

关于c++ - strtok 或 std::istringstream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426642/

相关文章:

c++ - 等到函数完成后再继续 main(一个线程)

c - 如何在c中分割嵌套的分隔字符串?

C++ strtok - 多次使用更多数据缓冲区

c++ - 多视角人脸检测

c++ - 动态工具提示 : TTN_GETDISPINFO not send when using LPSTR_TEXTCALLBACK

c - *str 和 atoi(str) 的区别

C++ - 重复使用 istringstream

c++ - 在模板函数中从 istream 中提取 bool

c++ - 从字符串中检测数字而不是字母的方法。 (目前使用istringstream)

c++ - Makefile c++11 支持