- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个最小的程序使用 boost::program_options
来解析 stringstream
。奇怪的是,在解析之后,流不再处于“良好”状态,并且设置了 failbit 和 eofbit。
#include <iostream>
#include <sstream>
#include <boost/program_options/options_description.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/variables_map.hpp>
void test_stream(std::stringstream& s);
int main()
{
using namespace std;
namespace po = boost::program_options;
stringstream s;
s << "seed=3" << '\n';
test_stream(s);
po::options_description desc("");
desc.add_options()
("seed", po::value<int>());
po::variables_map vm;
po::store(po::parse_config_file(s, desc, true), vm);
po::notify(vm);
test_stream(s);
return 0;
}
void test_stream(std::stringstream& s)
{
using namespace std;
if (s.good())
{
cout << "stream is good" << endl;
}
else
{
cout << "stream is not good" << endl;
if (s.rdstate() & ios_base::badbit)
cout << "badbit is set" << endl;
if (s.rdstate() & ios_base::failbit)
cout << "failbit is set" << endl;
if (s.rdstate() & ios_base::eofbit)
cout << "eofbit is set" << endl;
}
}
输出:
stream is good
stream is not good
failbit is set
eofbit is set
虽然 eof 条件在某种程度上是预期的,但由于推测解析器已读取流直到 EOF,为什么还设置了 failbit?
最佳答案
根据 ios::eof
的文档标记在某些情况下可能会发生这种情况:
Operations that attempt to read at the End-of-File fail, and thus both the eofbit and the failbit end up set. This function can be used to check whether the failure is due to reaching the End-of-File or to some other reason.
Boost 的解析器使用 std::copy()
和流上的迭代器来提取选项。正如 Jerry Coffin's answer 中指出的那样对于另一个问题,迭代器从序列中读取项目。读取最后一个后,序列的 eof
位被设置。当迭代器再次递增以获得流结束迭代器时,这是在 copy()
中离开循环的条件,它会尝试再次读取,因此也是流的 设置失败
位。
关于c++ - 为什么 parse_config_file 在流上设置 failbit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56582946/
对于我的一项作业,我被告知使用 cin.clear(ios_base::failbit) 来设置 failbit。我想知道,cin.clear(ios_base::failbit) 和 cin.set
创建一个文件并用零填充它: dd if=/dev/zero of=/tmp/zeroes count=1 编写这个小程序来提取它在文件中遇到的第一个无符号整数。 #include #include
我正在尝试编写一个程序,要求使用 cin.getline() 输入一个 char 数组,如果给定的输入大于数组长度,数组将被扩展。 我使用 cin.failbit 来查看用户的输入是否太长。但一切都不
#include #include #include using namespace std; int main(){ istringstream input("1234");
这个最小的程序使用 boost::program_options 来解析 stringstream。奇怪的是,在解析之后,流不再处于“良好”状态,并且设置了 failbit 和 eofbit。 #in
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我读了一个question在 Stack Overflow 上,istream file 的 eofbit 已设置,但 failbit 未设置。在这种情况下,file 为真且 file.eof() 为
我无法读取二进制文件。它似乎没有读到最后: // get file size ifs.open (inFile.c_str(), ios::binary | ios::ate); cout > c;
设置故障位后:当我先调用 cin.clear() 然后调用 cin.ignore() 时,程序是正确的。当我先调用 cin.ignore() 然后调用 cin.clear() 时,忽略似乎不起作用,为
是否可以使用 getline() 读取有效文件而不设置 failbit?我想使用 failbit 以便在输入文件不可读时生成异常。 以下代码总是在最后一行输出 basic_ios::clear - 即
对于以下代码: #include #include using namespace std; int main() { istringstream iss("a"); iss.ge
我正在创建一个原始类型包装器,它可以使用 boost::lexical_cast 从字符串设置它的值。它工作正常,但由于某种原因 std::istream 提取运算符设置了 failbit。以下程序打
我正在尝试使用 ios 包读取 csv 文件,但没有读取任何内容。我检查了文件流中的 failbit,由于某种原因,它在我实际读取任何内容之前就被立即设置了。这是我的代码: #include #in
我试图使用 std::ifstream 将整个文件读入缓冲区,但没有明显的原因失败,所以我构建了一个最小的代码示例来演示问题: std::vector vec; vec.resize(1000); s
我正在尝试使用 c++ std::ifstream 类将二进制文件准备成一组变量。 以下示例有效: std::ifstream inFile; inFile.open("example.bin");
我知道这种行为的起源,因为它已经在 SO 的多个帖子中得到了很好的解释,一些值得注意的例子是: Why is iostream::eof inside a loop condition conside
是否应该使用 std::ws 操纵器从流中提取失败位?在以下代码中,Clang 编译的(在 Xcode 4.5.1 中)程序最终断言失败。显然 s >> std::ws 在 EOF 导致失败。然而 G
当我将包含 long double 的 string 转换为 float 或 double 时,std: :istringstream 不会在 QNX 中引发 failbit 标志。 下面是演示代码:
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Failing to read file loaded with ifstream 以下文件的输出是:001
我看到了 istream::seekg 的奇怪行为。具体来说,它似乎当我寻找明显远离文件末尾的点时,不要设置故障位。 根据我在文档中了解到的信息,应该设置故障位,但实际上并没有。 谁能解释这种行为?相
我是一名优秀的程序员,十分优秀!