文件流tellg / tellp和gcc-4.6是这个bug吗? [英] file stream tellg/tellp and gcc-4.6 is this a bug?
问题描述
此代码:
#include< iostream&
#include< cstdio>
#include< fstream>
#include< string>
int main()
{
std :: remove(test.txt);
std :: fstream f(test.txt,std :: ios :: in | std :: ios :: out | std :: ios :: binary | std :: ios :: trunc);
std :: cout<< f.good()<< std :: endl;
f<<test<< std :: flush;
std :: cout<< f.tellg()<< < f.tellp()<< std :: endl;
f.seekg(0);
std :: string s;
f>> s;
std :: cout<< f.tellg()<< < f.tellp()<< std :: endl;
}
在gcc-4.4.5中输出以下输出
1
4 4
4 4
ie ./gcc-4.6.0
Gives:
p> 1
4 4
-1 4
我在哪里可以找到引用:
- (gcc-4.6中的错误)
- 第二种情况正确(gcc< gcc-4.6中的错误)
- undefined
确定,这不是一个错误, :
根据C ++ 2003标准:
-
tellg :(27.6.1.3)
在构造一个sentry对象之后,如果fail()!= false,则返回pos_type(-1)失败。否则,返回rdbuf() - > pubseekoff(0,cur,in)。
-
sentry(27.6.1.1.2 ):
if noskipws is zero and is.flags()& ios_base :: skipws非零,func-
提取并丢弃每个字符,只要下一个可用的输入字符c是空格字符。如果is.rdbuf() - > sbumpc()或is.rdbuf() - > sgetc()返回traits :: eof(),函数调用setstate(failbit | eofbit)(可能会抛出ios_base :: failure) / p>
所以基本上
- tellg()创建sentry对象
- sentry提取空格字符,并在获取eof后设置failbit。
- tellg()看到failbit应该返回eof()(-1)
所以gcc-4.6似乎行为正确...
This code:
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
int main()
{
std::remove("test.txt");
std::fstream f("test.txt",std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc);
std::cout << f.good() << std::endl;
f<<"test"<< std::flush;
std::cout << f.tellg() << " " << f.tellp() << std::endl;
f.seekg(0);
std::string s;
f>>s;
std::cout << f.tellg() << " " << f.tellp() << std::endl;
}
Gives following output in gcc-4.4.5
1
4 4
4 4
i.e. both tellg and tellp returned expected stream position 4.
While gcc-4.6.0
Gives:
1
4 4
-1 4
Where can I find a reference to tell:
- 1st case is correct (bug in gcc-4.6)
- 2nd case is correct (bug in gcc < gcc-4.6)
- Both case are correct the behavior is undefined
Ok, it is not a bug, even it seems that it is required behavior:
According to C++ 2003 standard:
tellg(): (27.6.1.3)
After constructing a sentry object, if fail() != false, returns pos_type(-1) to indicate failure. Otherwise, returns rdbuf()->pubseekoff(0, cur, in).
sentry (27.6.1.1.2):
if noskipws is zero and is.flags() & ios_base::skipws is nonzero, the func- tion extracts and discards each character as long as the next available input character c is a whitespace character. If is.rdbuf()->sbumpc() or is.rdbuf()->sgetc() returns traits::eof(), the function calls setstate(failbit | eofbit) (which may throw ios_base::failure).
So basically
- tellg() creates sentry object:
- sentry extracts white space characters and should set failbit after getting to eof.
- tellg() sees failbit should return eof() (-1)
So gcc-4.6 seems to behave correctly...
这篇关于文件流tellg / tellp和gcc-4.6是这个bug吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!