如何用 C++ 獲取檔案的大小?


要獲取 C++ 中的檔案大小,首先開啟檔案並將其查詢至結尾。tell() 將告訴我們流的當前位置,該位置是檔案中的位元組數。

示例

 演示

#include<iostream>
#include<fstream>
using namespace std;
int main() {
   ifstream in_file("a.txt", ios::binary);
   in_file.seekg(0, ios::end);
   int file_size = in_file.tellg();
   cout<<"Size of the file is"<<" "<< file_size<<" "<<"bytes";
}

輸出

Size of the file is 44 bytes

更新日期:2019 年 7 月 30 日

14K+ 次瀏覽

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.