read()方法從緩沖區(qū)或設備讀取指定長度的字節(jié)數(shù),返回對自身的引用.
而readsome()方法只能從緩沖區(qū)中讀取指定長度字節(jié)數(shù),并返回實際已讀取的字節(jié)數(shù).
比如:
const int LEN = 20;


char chars[ LEN + 1 ] =
{0};
ifstream in( fileName );

in.read( chars, LEN );
cout << chars << endl;

in.readsome( chars, LEN );
cout << chars << endl;
其中
in.read( chars, LEN );
將文件從設備載入緩沖區(qū),并讀取LEN長度.
接下來
in.readsome( chars, LEN );
就可以從緩沖區(qū)中讀取.
在緩沖區(qū)中沒有數(shù)據(jù)時,用readsome()得不到任何數(shù)據(jù).
而有時候想要從設備讀取指定長度的數(shù)據(jù),但又要知道實際讀取的長度,這時候就要用另一個方法: gcount()
它返回自上次讀取以來所讀取的字節(jié)數(shù),因此可以這樣得到實際讀取的長度.
int count = 0;

in.read( chars, LEN );
count = in.gcount();

cout << "Read in " << count << " chars : " << chars << endl;
實際上,readsome()也是調(diào)用read()和gcount()來實現(xiàn)的.
可冰 2005-08-15 14:29
文章來源:http://kb.cnblogs.com/archive/2005/08/15/215346.html