- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
是否调用 ifstream::close
重置流的 failbit
和/或 badbit
,类似于调用 clear
?这不是 this question 的拷贝- 我需要知道标志是否被重置,而不仅仅是它们何时被设置。
例如,我在当前项目中使用类似以下 if-else
的东西:
ifstream myInputStream("foo.txt");
//Let's pretend this compiles all the time, even though it
//might not because the operator is ambiguous.
myInputStream << "Outputting to an input stream causes problems."
if (myInputStream.fail())
{
cout << "Aw, nuts. It failed." << endl;
myInputStream.close();
return false;
}
else
{
cout << "No nuts here. Only chocolate chips." << endl;
myInputStream.close();
return true;
}
我是否必须在每个分支调用 myInputStream.fail
之后调用 myInputStream.close
以获得准确的检查?或者这行得通吗:
ifstream myInputStream("foo.txt");
myInputStream << "Outputting to an input stream causes problems.";
myInputStream.close();
if (myInputStream.fail())
{
cout << "Aw, nuts. It failed." << endl;
return false;
}
else
{
cout << "No nuts here. Only chocolate chips." << endl;
return true;
}
我知道ifstream::close
如果关闭失败,它本身可以设置 failbit
或 badbit
,这是我想在检查失败之前调用它的原因之一——我需要返回 false不管是什么原因造成的。如果闭包只执行一次,它看起来也不那么困惑。
是否ifstream::close
reset failbit
或 badbit
如果其他东西已经设置了它,使我的第二个代码示例返回 true?
最佳答案
close()
不应清除任何状态标志,尽管它可能会根据底层缓冲区的返回值设置 failbit
。
[fstream.members](还有 [ifstream.members] 和 [ofstream.members])
void close();
Effects: Calls
rdbuf()->close()
and, if that function returns returns a null pointer, callssetstate(failbit)
(27.5.5.4) (which may throwios_base::failure
).
标志被open()
清除,但是,假设 filebuf 正确打开
void open(const char* s, ios_base::openmode mode =
ios_base::in|ios_base::out);Effects: Calls
rdbuf()->open(s,mode)
. If that function does not return a null pointer callsclear()
, otherwise callssetstate(failbit)
, (which may throwios_base::failure
) (27.5.5.4).
关于c++ - ifstream::close 是否会重置 failbit 和/或 badbit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32833632/
根据 C++11 标准,是 的行为 ifstream in("."); 指定的,还是系统相关的? 对于上下文,我试图避免使用 boost::filesystem 和类似的库,因为它们会导致不相关的可移
我尝试使用 ifstream 读取一个 3GB 的数据文件,但它给出了错误的文件大小,而当我读取一个 600MB 的文件时,它给出了正确的结果。除了错误的文件大小,我也无法使用 ifstream 读取
这里有没有人知道 C++ ifstream 的 get 指针在调用 read() 后可能会损坏的方法?我看到了一些我无法解释的真正奇怪的行为。例如(说明性代码,而不是我实际运行的代码): int ma
我正在编写一个外部合并排序。它是这样工作的:从大文件中读取 k 个 block ,在内存中对它们进行排序,执行 k 路合并,完成。所以我需要在 k-way 合并阶段从文件的不同部分顺序读取。最好的方法
阅读 Savitch 的 Problem Solving in C++,std::ifstream::fail 以检查文件是否已正确打开(ifstream 或 ofstream)。 正如我第一次看到的
我正在尝试逐行读取多个文件(本例中为 3 个)并使用 ifstream shared_ptrs vector 来执行此操作。但我不知道如何取消引用此指针以使用 getline() 或我的代码中存在其他
我正在编写一个程序,它从一个文本文件中获取多个变量。当程序发现EOF时, 它结束输入数据。 int main() { int val, count = 0; ifstream file
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我想知道在什么情况下我们可以拥有: bool(std::ifstream) != std::ifstream::good() 区别在于 bool(std::ifstream) 不测试 eof 位,而
也许是个伪问题,但我需要一个明确的答案。这些函数的返回有什么不同吗 int FileExists(const std::string& filename) { ifstream file(file
我只是采用了一些在 Linux 下开发的文件阅读器的旧代码,并尝试在我的 Windows 项目中使用完全相同的代码,该项目是用 MSVC++7.1 编译的。代码编译没有任何问题,但根据 Windows
是var > var相同? 据我所知,它们应该完全相同。但是已经很晚了,我的大脑处于半睡状态,所以我想澄清一下。 最佳答案 它们不一样。 foo > foo是bar.operator>>(foo)或
我一直在测试使用 Visual Studio 在 C++ 中读取文件的性能,我得到了一些我真的不明白的结果。 我的代码如下: std::vector Method1(std::string fileP
我使用 SymbolHound 在网络上查找比较两者的资源,但找不到任何东西。 在 VS13 中寻找 std::ifstream::in 的声明和定义让我找到了 basic_ifstream。寻找 s
我无法将图像文件读入缓冲区。当我读取普通的 ascii 文件时,一切都很好,但当涉及到图像文件时,我怀疑图像文件中有一个\0 字符? 我需要接收图像文件,将其解析为 16KB 的 block 以进行哈
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
首先,我为我的英语表示歉意。 我在尝试编写一个 XML 解析器时遇到了一个奇怪的问题。为了解释我的问题,我应该说,我有一个 xml 解析器类,它有一个 ifstream 成员。这个类有一个函数,它会读
我正在使用 xcode。我编译得很好,然后一旦它运行它就成功打开文件,然后读取两个值,但这些值不会进入变量,因此它会跳过 for 循环并关闭文件并从 main 返回。 #include #inclu
我有一个包含以下格式数据的文件: name1 p1 p2 ... p11 name2 p1 p2 ... p11 ... (参数不一定在一行) 我的目标是读取名称和 11 个参数,对它们做一些事情,然
我正在尝试读取一个输入文件,该文件的格式为一行中有两个数字,并将该行中的第一个数字存储在一个 vector 中,将第二个数字存储在另一个 vector 中。 除了实际读取文件之外,我的代码的每个部分都
我是一名优秀的程序员,十分优秀!