增加一个streampos对象
我正在尝试做这样的事情:
for (std::streampos Position = 0; Position < 123; Position++)
{
// Use Position to access something...
}
但是,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:
是否有任何解决方法,或者我是否必须依赖 long unsigned int
足够大对于文件?
I'm trying to do something like this:
for (std::streampos Position = 0; Position < 123; Position++)
{
// Use Position to access something...
}
However, it appears that std::streampos
does not have operator++
overloaded.
Trying to use Position = (Position + 1)
results in the following error:
ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
Is there any workaround for this, or do I have to rely on long unsigned int
being big enough for files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试
std::streamoff
,它表示偏移量在一个流中。它支持前置和后置递增/递减运算符。编辑Maxpm的评论:您可以应用
streamoff
到任何地方,无论是ios::beg
还是任意streampos
。将其应用于 ios::beg ,它的行为就像普通的streampos 。将其应用到streampos
上,您就得到了streampos+streamoff
。Try a
std::streamoff
, which represents an offset in a stream. It supports both pre- and post increment/decrement operators.Edit to Maxpm's comment: You can apply the
streamoff
to anywhere, be itios::beg
or an arbitarystreampos
. Apply it toios::beg
and it behaves like a normalstreampos
. Apply it to astreampos
and you gotstreampos+streamoff
.使用
+=
:+
不起作用,因为operator +
实际上是为streampos
和steamoff 定义的
,而不是int
。这意味着存在两个同样好的隐式转换:您的
1
可以转换为streamoff
(这可能是unsigned long
的 typedef )。或者将streampos
隐式转换为streamoff
,然后添加1
。Use
+=
:+
doesn’t work becauseoperator +
is actually defined forstreampos
andsteamoff
, notint
.This means that two implicit conversions exist that are equally good: either your
1
could be converted tostreamoff
(which is probably a typedef forunsigned long
). Or thestreampos
is implicitly converted tostreamoff
which then has1
added.std::streampos
不是数字类型,尽管它支持转换与数字类型之间的转换。如果你想对
位置,您需要使用
std::streamoff
(并指定 from调用seek时的参数)。
另外,不要忘记,你不能在其中寻求任意位置
一个文件,除非它是以二进制模式打开的,并且带有“C”
语言环境。
std::streampos
is not a numeric type, although it supports conversionto and from numeric types. If you want to do arithmetic on the
position, you need to use
std::streamoff
(and specify the fromargument when calling seek).
Also, don't forget that you can't seek to an arbitrary position in
a file unless it has been opened in binary mode, and imbued with the "C"
locale.
我在研究如何从
std::streampos
对象中减去时遇到了这个链接:https://bugs.debian.org/cgi-bin/bugreport。 cgi?bug=187599
建议的解决方案是使用定义的运算符重载
operator+()
或operator-()
,它们非常适用于 我。聚苯乙烯
使用
operator+()
可能比使用operator-()
更好,以避免与符号更改相关的任何问题I came across this link while looking into how I can subtract from a
std::streampos
object:https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=187599
The suggested solution is using the defined operator overload
operator+()
oroperator-()
which work pretty well for me.P.S.
It may be better to use
operator+()
thanoperator-()
to avoid any problems related to sign change