(原創) 如何得知檔案大小? (C/C++)
1
/*
2
(C) OOMusou 2007 http://oomusou.cnblogs.com
3
4
Filename : ArrayCopyCout.cpp
5
Compiler : Visual C++ 8.0 / BCB 6.0 / gcc 3.4.2 / ISO C++
6
Description : Demo how to get file size
7
Release : 03/04/2007 1.0
8
*/
9
#include <iostream>
10
#include <fstream>
11
12
using namespace std;
13
int main() {
14
ifstream file("clena.bmp",ios::in | ios::binary | ios::ate);
15
ifstream::pos_type eof = file.tellg();
16
cout << "file size : " << eof << "bytes" << endl;
17
18
file.close();
19
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

執行結果

ios::ate會將檔案指標移到檔尾,透過ifstream::tellg()得知目前檔案指標位置,即相當於檔案大小byte數。
或許C++還有更好的方法,這是我目前學習ifstream時得知的方式,若有更好的方式歡迎指正,謝謝。