C++ STL中的對文件操作的類
ifstream 用于讀文件
ofstream 用于寫文件
fstream 用于讀寫文件
打開文件
可以在夠高文件流對象時直接打開
ifstream ifile( 文件名 )
ofstream ofile( 文件名 )
fstream file( 文件名 )
也可以用open行為
ifstream ifile
ifile.open( 文件名 )
ofstream ofile
ofile.open( 文件名 )
fstream file
file.open( 文件名 )
關閉文件
文件對象銷毀時自動關閉文件。
也可用close關閉文件。
ifile.close()
ofile.close()
file.close()
文件大開放式標致
這寫標致定義在iso_base類中。分別如下
in 打開,用于讀取(這是ifstream的缺省模式)
out 打開,用于改寫(這是ofstream的缺省模式)
app 寫入是始終添加與尾端
ate 打開文件之后令讀寫位置移至文件尾端
trunc 將先前的文件內容移除
binary 二進制方式打開
這些標致和或在一起。
這些標致作為對象構造或open行為的第二個參數,來定義文件打開分方式。
隨機存儲
用于讀文件的隨機存儲
tellg() 返回讀取的位置
seekg( pos ) 從當前位置移動pos個位子(絕對移送)
seekg( offset, rpos ) 以rpos位置開始移動offset個位置(相對移動)
用于寫文件的隨機存儲
tellp() 返回寫入的位置
seekp( pos ) 從當前位置移動pos個位子(絕對移送)
seekp( offset, rpos ) 以rpos位置開始移動offset個位置(相對移動)
讀數據
利用read行為
ifstream ifile
ifile.read(buf,length)
寫數據
利用write行為
ofstream ofile
ofile.write(buf,length)
posted on 2007-05-02 13:31
walkspeed 閱讀(1558)
評論(0) 編輯 收藏 引用 所屬分類:
C++語言