为什么 parse_config_file 在流上设置 failbit?
Why does parse_config_file set failbit on a stream?
这个最小的程序使用 boost::program_options
来解析 stringstream
。奇怪的是,解析后,流不再处于 "good" 状态,并且 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()
中离开循环的条件,它会尝试再次读取,因此也会设置流的 fail
位.
这个最小的程序使用 boost::program_options
来解析 stringstream
。奇怪的是,解析后,流不再处于 "good" 状态,并且 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()
中离开循环的条件,它会尝试再次读取,因此也会设置流的 fail
位.