How can I get a file’s size in C++? [duplicate]

#include <fstream> std::ifstream::pos_type filesize(const char* filename) { std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); return in.tellg(); } See http://www.cplusplus.com/doc/tutorial/files/ for more information on files in C++. edit: this answer is not correct since tellg() does not necessarily return the right value. See http://stackoverflow.com/a/22986486/1835769

java get file size efficiently

Well, I tried to measure it up with the code below: For runs = 1 and iterations = 1 the URL method is fastest most times followed by channel. I run this with some pause fresh about 10 times. So for one time access, using the URL is the fastest way I can think of: … Read more

Get human readable version of file size?

Addressing the above “too small a task to require a library” issue by a straightforward implementation (using f-strings, so Python 3.6+): def sizeof_fmt(num, suffix=”B”): for unit in [“”, “Ki”, “Mi”, “Gi”, “Ti”, “Pi”, “Ei”, “Zi”]: if abs(num) < 1024.0: return f”{num:3.1f}{unit}{suffix}” num /= 1024.0 return f”{num:.1f}Yi{suffix}” Supports: all currently known binary prefixes negative and positive … Read more

How do you determine the size of a file in C?

On Unix-like systems, you can use POSIX system calls: stat on a path, or fstat on an already-open file descriptor (POSIX man page, Linux man page). (Get a file descriptor from open(2), or fileno(FILE*) on a stdio stream). Based on NilObject’s code: #include <sys/stat.h> #include <sys/types.h> off_t fsize(const char *filename) { struct stat st; if … Read more