Just My Life & My Work

寫程式來讀取檔案很少用到,
但卻是個相當有用的技巧,
以下四個步驟的程式碼:

/**
	Theme: Read Size of File 
	compiler: Dev C++ 4.9.9.2
	Date: 100/04/03
	Author: ShengWen
	Blog: https://cg2010studio.wordpress.com/
*/
#include<iostream>
#include<fstream>
using namespace std;

int main(){
    // 1. 開檔
    ifstream fin("Read Size of File.cpp", ifstream::in | ifstream::binary);
    // 2. Seek 檔案頭到尾
    fin.seekg(0, ios::end);
    // 3. tell 位置 
    long long length = fin.tellg();
    // 4. 關檔
    fin.close();
    cout << length << " bytes." << endl;
    system("pause");
	return 0;
}

以讀取本檔為例,
輸出為:

542 bytes.
請按任意鍵繼續 . . .

改掉字串Read Size of File.cpp為你的檔案名稱與副檔名即可。

隨意留個言吧:)~

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料

標籤雲