파일 사이즈 구하는 방법은 두 종류다.
- stat() 함수를 사용하는 방법
- 파일 포인터를 이용하는 방법
C++에서 파일 포임터를 이용하는 방법은 다음과 같다.
// read a file into memory #include <iostream> #include <fstream> using namespace std; int main () { int length; char * buffer; ifstream is; is.open ("test.txt", ios::binary ); // get length of file: is.seekg (0, ios::end); length = is.tellg(); is.seekg (0, ios::beg); // allocate memory: buffer = new char [length]; // read data as a block: is.read (buffer,length); is.close(); cout.write (buffer,length); delete[] buffer; return 0; }