std :: ifstream :: read或std :: ofstream :: write与零参数? [英] std::ifstream::read or std::ofstream::write with a zero parameter?
问题描述
它是完全确定(=根据标准良好定义的行为)调用:
Is it perfectly ok (= well defined behaviour according to the standard) to call :
mystream.read(buffer, 0);
或
mystream.write(buffer, 0);
(当然不会读取或写入任何内容)。
我想知道如果在调用这两个函数之一之前,我必须测试所提供的大小是否为null。
(and of course nothing will be read or written). I would like to know if I have to test if the provided size is null before calling one of these two functions.
推荐答案
是的,行为是明确定义的:两个函数将通过未格式化的输入/输出函数的运动(构造哨兵,设置failbit如果eofbit设置,刷新绑定流,如果必要),然后他们将获得到此子句:
Yes, the behavior is well-defined: both functions will go through the motions for unformatted input/output functions (constructing the sentry, setting failbit if eofbit is set, flushing the tied stream if necessary), and then they will get to this clause:
§27.7.2.3[istream.unformatted] / 30
§27.7.2.3[istream.unformatted]/30
提取字符并存储,直到发生以下任一情况:
Characters are extracted and stored until either of the following occurs:
- 存储n个字符;
— n characters are stored;
§27.7.3.7[ostream.unformatted] / 5
§27.7.3.7[ostream.unformatted]/5
字符插入,直到发生以下任一情况
Characters are inserted until either of the following occurs
- 插入n个字符;
— n characters are inserted;
插入在存储或提取任何内容之前为真。
"zero characters are stored/inserted" is true before anything is stored or extracted.
看一下实际的实现,我看到 for(; gcount< n; ++ gcount)
在libc ++ 或 sgetn(buffer,n);
in stdlibc ++ ,其中包含等效循环
Looking at actual implementations, I see for (; gcount < n; ++gcount)
in libc++ or sgetn(buffer, n);
in stdlibc++ which has the equivalent loop
这篇关于std :: ifstream :: read或std :: ofstream :: write与零参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!