便携式的方式来获取文件大小在C / C ++ [英] Portable way to get file size in C/C++

查看:240
本文介绍了便携式的方式来获取文件大小在C / C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定文件的字节大小。

I need to determin the byte size of a file.

编码语言是C ++,代码应该与Linux,Windows和任何其他操作系统配合使用。这意味着使用标准的C或C ++函数/类。

The coding language is C++ and the code should work with Linux, windows and any other operating system. This implies using standard C or C++ functions/classes.

这种琐碎的需求显然没有琐碎的解决方案。

This trivial need has apparently no trivial solution.

推荐答案

使用std的流可以使用:

Using std's stream you can use:

std::ifstream ifile(....);
ifile.seekg(0, std::ios_base::end);//seek to end
//now get current position as length of file
ifile.tellg();

如果你处理只写文件(std :: ofstream),那么方法是另一种: / p>

If you deal with write only file (std::ofstream), then methods are some another:

ofile.seekp(0, std::ios_base::end);
ofile.tellp();

这篇关于便携式的方式来获取文件大小在C / C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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