Max*_*xpm 6 c++ operator-overloading filestream
我正在尝试做这样的事情:
for (std::streampos Position = 0; Position < 123; Position++)
{
// Use Position to access something...
}
Run Code Online (Sandbox Code Playgroud)
但是,它似乎std::streampos没有operator++超载.
尝试使用Position = (Position + 1)导致以下错误:
ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
Run Code Online (Sandbox Code Playgroud)
有没有解决方法,或者我必须依靠long unsigned int足够大的文件?
用途+=:
for (std::streampos Position = 0; Position < 123; Position += 1)
Run Code Online (Sandbox Code Playgroud)
+因为不工作operator +的实际定义streampos和steamoff,没有int。
这意味着存在两个同样不错的隐式转换:您1可以将转换为streamoff(可能是typedef unsigned long)。或将streampos其隐式转换为streamoff已1添加的内容。
尝试a std::streamoff,表示流中的偏移量.它支持前后增量/减量运算符.
底层类型是实现定义的,但必须能够一致地转换为streamsize和fpos(因此,也是streampos)
编辑到Maxpm的评论:您可以将其streamoff应用于任何地方,无论是它ios::beg还是仲裁streampos.应用它ios::beg,它的行为就像正常一样streampos.把它应用到a streampos,你得到了streampos+streamoff.
std::streampos不是数字类型,尽管它支持与数字类型之间的转换。如果要对位置进行算术运算,则需要使用std::streamoff(并在调用 seek 时指定 from 参数)。
另外,不要忘记你不能在文件中寻找任意位置,除非它以二进制模式打开,并且充满了“C”语言环境。