如何在 C++ 中获取文件的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5840148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I get a file's size in C++?
提问by Sophie Sperner
Let's create a complementary question to this one. What is the most common way to get the file size in C++? Before answering, make sure it is portable (may be executed on Unix, Mac and Windows), reliable, easy to understand and without library dependencies (no boost or qt, but for instance glib is ok since it is portable library).
让我们为这个问题创建一个补充问题。在 C++ 中获取文件大小的最常用方法是什么?在回答之前,请确保它是可移植的(可以在 Unix、Mac 和 Windows 上执行)、可靠、易于理解并且没有库依赖(没有 boost 或 qt,但例如 glib 是可以的,因为它是可移植的库)。
回答by Spyros
#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++.
有关C++ 文件的更多信息,请参阅http://www.cplusplus.com/doc/tutorial/files/。
edit: this answer is not correct since tellg() does not necessarily return the right value. See http://stackoverflow.com/a/22986486/1835769
编辑:这个答案不正确,因为 tellg() 不一定返回正确的值。见http://stackoverflow.com/a/22986486/1835769
回答by Matt
While not necessarily the most popular method, I've heard that the ftell, fseek method may not always give accurate results in some circumstances. Specifically, if an already opened file is used and the size needs to be worked out on that and it happens to be opened as a text file, then it's going to give out wrong answers.
虽然不一定是最流行的方法,但我听说 ftell、fseek 方法在某些情况下可能并不总是给出准确的结果。具体来说,如果使用了一个已经打开的文件并且需要计算出它的大小,而它恰好是作为文本文件打开的,那么它会给出错误的答案。
The following methods should always work as stat is part of the c runtime library on Windows, Mac and Linux.
以下方法应始终有效,因为 stat 是 Windows、Mac 和 Linux 上 c 运行时库的一部分。
long GetFileSize(std::string filename)
{
struct stat stat_buf;
int rc = stat(filename.c_str(), &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
or
long FdGetFileSize(int fd)
{
struct stat stat_buf;
int rc = fstat(fd, &stat_buf);
return rc == 0 ? stat_buf.st_size : -1;
}
On some systems there is also a stat64/fstat64. So if you need this for very large files you may want to look at using those.
在某些系统上还有一个 stat64/fstat64。因此,如果您需要对非常大的文件使用此功能,您可能需要考虑使用它们。
回答by bames53
Using the C++ filesystem TS:
使用 C++ 文件系统 TS:
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
int main(int argc, char *argv[]) {
fs::path p{argv[1]};
p = fs::canonical(p);
std::cout << "The size of " << p.u8string() << " is " <<
fs::file_size(p) << " bytes.\n";
}
回答by Andro
It is also possible to find that out using the fopen(),fseek() and ftell() function.
也可以使用 fopen()、fseek() 和 ftell() 函数找出这一点。
int get_file_size(std::string filename) // path to file
{
FILE *p_file = NULL;
p_file = fopen(filename.c_str(),"rb");
fseek(p_file,0,SEEK_END);
int size = ftell(p_file);
fclose(p_file);
return size;
}
回答by Sharon Joseph
#include <stdio.h>
int main()
{
FILE *f;
f = fopen("mainfinal.c" , "r");
fseek(f, 0, SEEK_END);
unsigned long len = (unsigned long)ftell(f);
printf("%ld\n", len);
fclose(f);
}
回答by Hadi Rasekh
In c++ you can use following function, it will return the size of you file in bytes.
在 C++ 中,您可以使用以下函数,它将以字节为单位返回文件的大小。
#include <fstream>
int fileSize(const char *add){
ifstream mySource;
mySource.open(add, ios_base::binary);
mySource.seekg(0,ios_base::end);
int size = mySource.tellg();
mySource.close();
return size;
}
回答by Sammy
The code snippet below exactly addresses the question in this post :)
下面的代码片段正好解决了这篇文章中的问题:)
///
/// Get me my file size in bytes (long long to support any file size supported by your OS.
///
long long Logger::getFileSize()
{
std::streampos fsize = 0;
std::ifstream myfile ("myfile.txt", ios::in); // File is of type const char*
fsize = myfile.tellg(); // The file pointer is currently at the beginning
myfile.seekg(0, ios::end); // Place the file pointer at the end of file
fsize = myfile.tellg() - fsize;
myfile.close();
static_assert(sizeof(fsize) >= sizeof(long long), "Oops.");
cout << "size is: " << fsize << " bytes.\n";
return fsize;
}