确定 std::istream 长度的更好方法? [英] Better way to determine length of a std::istream?

查看:55
本文介绍了确定 std::istream 长度的更好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有比以下更好的方法来确定 std::istream 的长度:

Is there a better way to determine the length of an std::istream than the following:

std::istream* pcStream = GetSomeStream();
pcStream->seekg(0, ios::end);
unsigned int uiLength = pcStream->tellg();

必须寻找流的末尾然后返回原始位置似乎真的很浪费,特别是如果流可能是某些慢速媒体(如 CD 或 DVD)上的文件.

It just seems really wasteful to have to seek to the end of the stream and then seek back to the original position, especially if the stream might be to a file on some slow media like a CD or DVD.

推荐答案

最好"的方法是避免需要长度 :)

The "best" way is to avoid needing the length :)

  • 并非所有流都是可查找的(例如,想象网络套接字上的 istream)
  • tellg() 的返回类型不一定是数字(唯一的要求是可以传回 seekg() 返回到同一个位置)
  • 即使是数字,也不一定是字节数.例如,它可能是一个magic"值,意思是最后"
  • 对于 fstreams,大小写和换行转换等问题可能会搞砸

这篇关于确定 std::istream 长度的更好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆