strtok or std::istringstream
strtok or std::istringstream
我有以下代码,它使用 strtok 从 txt 文件接收输入。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()
并且代码会更短,但我不喜欢类型不安全的界面)!我会和操纵者玩把戏:
#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";
}
}
}
相关文章:
- 使用std::multimap迭代器创建std::list
- 为什么当我将 std::locale 设置为 "zh_CN.UTF-8" 时 std::istringstream 失败?
- NDK r16b std::istringstream 的行为不符合预期
- 如何使用 std::istringstream 获取正确的缓冲区大小以进行数据解析
- 为什么std::istringstream在没有引发故障位标志的情况下生成错误的结果
- strtok or std::istringstream
- 如何从 std::istringstream 读取单词而不通过 >> 分配给 tmp 变量?
- 使用带有">>"运算符的 std::istringstream 的奇怪行为
- std::istringstream iss(std::move(string));越过初始化,尽管它是横向循环
- 如何保存和恢复 std::istringstream 的缓冲区?
- (C++)std::istringstream从字符串到双精度最多可读取6位数字
- c++std::istringstream怪异的std::hex行为
- 从"std::istringstream"初始化"const std::string"
- 为std::istringstream设置内部缓冲区
- 使用istringstream将std::string标记为std::map
- 如何理解std::istringstream的结果
- libc++ std::istringstream 不会抛出异常。错误?
- Std::istringstream.good()比预期多返回一次true
- 在循环外定义std::istringstream记录
- 如何从const unsigned char*初始化std::istringstream,无需强制转换或复制