- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(){
istringstream input("1234");
char c[5];
while(input.getline(c, 5, '\n')){
cout << "OUTPUT: " << c << endl;
}
}
输出是
OUTPUT: 1234
当我觉得所有消息来源都告诉我 input
应该测试为 false
并且应该没有输出时。来自标准 (N3337) [27.7.2.3]/18:
Effects: Behaves as an unformatted input function (as described in 27.7.2.3, paragraph 1). After constructing a sentry object, extracts characters and stores them into successive locations of an array whose first element is designated by s. Characters are extracted and stored until one of the following occurs:
- end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
- traits::eq(c, delim) for the next available input character c (in which case the input character is extracted but not stored);320
- n is less than one or n - 1 characters are stored (in which case the function calls setstate( failbit)).
由于存储了 4
值,因此应该设置 failbit
。其他一些来源对此函数的输入略有不同,但仍然令人困惑。 Cplusplus :
The failbit flag is set if the function extracts no characters, or if the delimiting character is not found once (n-1) characters have already been written to s. Note that if the character that follows those (n-1) characters in the input sequence is precisely the delimiting character, it is also extracted and the failbit flag is not set (the extracted sequence was exactly n characters long).
同样,在 4
之后没有找到定界字符 '\n'
,因此应该设置 failbit
。 Cppreference说了类似的话。我在这里缺少什么?
最佳答案
是的,它读取了 n-1
个字符,它从未遇到过 '\n'
但你错过了第一点
end-of-file occurs on the input sequence (in which case the function calls setstate(eofbit));
由于您准确读取了流中的内容,因此设置了 eofbit
并获得了输入。
如果我们添加
std::cout << input.eof();
你可以看到确实发生了什么( live example )
关于c++ - istream::getline failbit 没有设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34343455/
对于我的一项作业,我被告知使用 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 的奇怪行为。具体来说,它似乎当我寻找明显远离文件末尾的点时,不要设置故障位。 根据我在文档中了解到的信息,应该设置故障位,但实际上并没有。 谁能解释这种行为?相
我是一名优秀的程序员,十分优秀!