Determining File Size in C
How can I obtain the size of a file in C while maintaining portability, reliability, and simplicity, without incurring library dependencies?
Solution:
The most common and reliable method to determine file size in C is by utilizing the std::ifstream class. Here's how you can implement it:
#include <fstream> std::ifstream::pos_type filesize(const char* filename) { std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); return in.tellg(); }
In this code:
Note:
It's important to note that this solution uses tellg(), which doesn't always return the correct file size on certain systems. Refer to this discussion for additional information: http://stackoverflow.com/a/22986486/1835769.
The above is the detailed content of How to Determine File Size in C Without External Libraries?. For more information, please follow other related articles on the PHP Chinese website!