read()方法從緩沖區或設備讀取指定長度的字節數,返回對自身的引用.
而readsome()方法只能從緩沖區中讀取指定長度字節數,并返回實際已讀取的字節數.
比如:
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 );
將文件從設備載入緩沖區,并讀取LEN長度.
接下來
in.readsome( chars, LEN );
就可以從緩沖區中讀取.
在緩沖區中沒有數據時,用readsome()得不到任何數據.
而有時候想要從設備讀取指定長度的數據,但又要知道實際讀取的長度,這時候就要用另一個方法: gcount()
它返回自上次讀取以來所讀取的字節數,因此可以這樣得到實際讀取的長度.
int count = 0;

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

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