C++ find size of ofstream

浪子不回头ぞ 提交于 2019-12-05 20:02:29

You want tellp. This is available for output streams (e.g., ostream, ofstream, stringstream).

There's a matching tellg that's available for input streams (e.g., istream, ifstream, stringstream). Note that stringstream supports both input and output, so it has both tellp and tellg.

As to keeping the two straight, the p means put and the g means get, so if you want the "get position" (i.e., the read position) you use tellg. If you want the put (write) position, you use tellp.

Note that fstream supports both input and output, so it includes both tellg and tellp, but you can only call one of them at any given time. If the most recent operation was a write, then you can call tellp. If the most recent operation was a read, you can call tellg. Otherwise, you don't get meaningful results.

Use ostream::tellp(), before and after

fout::tellp() will give you position of the next byte to be written to.

You can get the current position using tellg or tellp:

fin.tellg();  // postition of "get" in istream or iostream (fstream)
fout.tellp(); // position of "put"  in ostream or iostream (fstream)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!