2011-04-30 49 views
97

我們來創建一個補充問題this one。 在C++中獲取文件大小的最常用方法是什麼? 在回答之前,確保它是可移植的(可以在Unix,Mac和Windows上執行), 可靠,易於理解且不依賴庫(無需增強或qt,但例如glib可以,因爲它是便攜式庫)。如何在C++中獲取文件的大小?

+3

的FSTAT功能的問題我相信是便攜式的,而且很容易使用。 – ultifinitus 2011-04-30 07:09:33

+0

可能與http://stackoverflow.com/questions/2409504 – 2011-04-30 07:10:58

+13

dupicated爲什麼沒有提升,但允許glib? Boost也是便攜式的。 – rve 2011-04-30 07:14:45

回答

124
#include <fstream> 

std::ifstream::pos_type filesize(const char* filename) 
{ 
    std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary); 
    return in.tellg(); 
} 

有關C++文件的更多信息,請參閱http://www.cplusplus.com/doc/tutorial/files/

+0

爲什麼你需要'std :: ifstream :: in'這裏?根本沒有輸入流。 – qed 2013-11-04 16:51:44

+31

更好的答案實際上是使用ios :: binary | ios :: ate代替。這將在最後以光標打開文件。所以你只需返回in.tellg()而不需要查找:) – jterm 2013-12-12 22:31:58

+3

或者只是從stat文件系統中獲取它的大小已被維護。無需打開文件。 – Matt 2014-06-11 20:06:56

22

使用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; 
} 
+4

您不需要''和'',您確實需要'',您需要進行錯誤檢查。 (使用NULL文件時'fseek'段錯誤,ftell在錯誤時返回-1) – rve 2011-04-30 16:48:04

+4

初始化p_file並在下一行覆蓋它是沒有意義的,並且使得許多lint抱怨「未使用的賦值」。 – Jens 2012-05-27 19:26:56

+0

你應該使用'long'作爲返回類型嗎? – Progo 2014-08-15 15:46:45

56

雖然不一定是最流行的方法,我聽說FTELL,FSEEK方法可能並不總是給在某些情況下準確的結果。具體來說,如果已經打開的文件被使用並且需要處理該文件的大小並且它恰好被打開爲文本文件,那麼它將會發出錯誤的答案。

由於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; 
} 

在某些系統上還有一個stat64/fstat64。所以,如果你需要這個非常大的文件,你可能想看看使用這些。

+2

上述代碼可以移植到C&C++ – Matt 2013-12-04 22:45:34

+2

+1中提到以二進制模式打開流。這解決了我在使用read()時使用fseek()+ ftell()大小的問題。 – 2014-11-07 20:14:09

+3

我需要添加#包括和GetFileSize(...)適用於我。 – SyntheticGio 2017-01-26 18:40:34

2

在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; 
} 
+0

這是行不通的,你不能將tellg輸出轉換爲int – 2018-01-25 10:29:29

+0

@StepanYakovenko看看這個網址http://www.cplusplus.com/reference/istream/istream/tellg/ – 2018-01-26 18:55:28

19

使用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"; 
} 
+3

值得注意的是'filesystem'是ISO C++的C++ 17 – chappjc 2016-12-10 02:17:47

+0

所以比tellg快嗎? – 2018-03-07 02:11:16

1
#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); 

} 
-1

正好下面的代碼片斷解決了在此 交:)

/// 
/// 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; 
} 
相關問題