freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

cio流-資料下載頁

2025-09-25 16:33本頁面
  

【正文】 st”); if (! out) { cout”cannot open output file .\n”。 return。 } double num=。 char str [ ]=”This is a test”。 ((char * )amp。num,sizeof (double ))。 (str, strlen (str))。 ( )。 } 程序執(zhí)行后,屏幕上不顯示任何信息,但程序已將雙精度 字符串“ this is a test” 以二進制形式寫入文件 test中。若用“寫字板”打開是看不出內(nèi)容的,用下面的程序 可以讀取文件test中的數(shù)據(jù),并在屏幕上顯示出來,以驗證前面的程序操作。 38 例 用 read( ) 函數(shù)讀取例 。 include include include void main( ) { ifstream in( ” test”) 。 if (! in) { cout”cannot open input file .\n”。 return。 } double num。 char str [80]。 ((char * )amp。num, sizeof (double))。 (str, 14)。 coutnum? ?str。 ( )。 } 程序運行結(jié)果為: This is a test 39 (3). 檢測文件結(jié)束, eof( )函數(shù)的使用 二進制文件的處理過程與文本文件的處理過程基本相同 , 但在判斷文件是否結(jié)束時有所區(qū)別 。 在文本文件中 , 遇到文件結(jié)束符 ,get函數(shù)返回一個文件結(jié)束標(biāo)志 EOF, 該標(biāo)志的值為 1。 但在處理二進制文件時 , 讀入某一字節(jié)中的二進制數(shù)的值可能是 1, 這與 EOF的值相同 。 這樣就有可能出現(xiàn)讀入的有用數(shù)據(jù)被處理成“ 文件結(jié)束 ” 的情況 。 為了解決這個問題 , c++提供了一個成員函數(shù) eof, 用來判斷文件是否真的結(jié)束 , 其原型為: int eof( ) ; 當(dāng)?shù)竭_文件末尾時 , 它返回一個非零值 , 否則返回零 。 當(dāng)輸入文件是鍵盤時 , 其結(jié)束符是 ctrl_z, eof( ) 函數(shù)返回的值為真 。 40 (4). 文件的隨機讀寫 前面介紹的文件操作都是按一定順序進行讀寫的 , 因此稱為順序文件 。 C++在類 istream及類 ostream中定義了幾個與在輸出流中隨機移動文件指針的成員函數(shù) , 則可以在流內(nèi)隨機移動文件指針 , 從而可以對文件的任意數(shù)據(jù)進行隨機讀寫 。 用函數(shù) seekg( )和 seekp( )進行隨機訪問 。 函數(shù) seekg( )用于輸入文件 , 函數(shù) seekp( )用于輸出文件 。 41 它們的 函數(shù)原型 如下: istream amp。seekg (streamoff offset, seek_dir origin)。 ostream amp。seekp (streamoff offset, seek_dir origin)。 其中,參數(shù) origin表示文件指針的 起始位置 , offset表示相對于這個起始位置的 位移量 。 seek_dir是一個系統(tǒng)定義的枚舉名, origin是枚舉變量。 origin的取值有以下三種情況: ? ios::beg 從文件頭開始,把文件指針移動由 offset指定的距離。offset的值為正數(shù) ? ios::cur 從當(dāng)前位置開始,把文件指針向前 (后 )移動由 offset指定的距離。 offset 的值為正或負(fù)數(shù),正數(shù)時向前移,負(fù)數(shù)時向后移。 ? ios::end 從文件尾開始,把文件指針移動由 offset指定的距離,offset的值為負(fù)數(shù)進行文件的隨機讀寫時,可以用以下函數(shù) 測試 文件指針的 當(dāng)前位置 : streampos tellg( );用于輸入文件。 streampos tellp( );用于輸出文件。 streampos是在 ,實際上是 long型的。 42 例 演示 seekp( ), seekg( )函數(shù)。它修改文件中的指定 (第 5)字符。 include include void main ( ) { fstream fs。 (“ ios::in|ios::out)。//以讀寫方式打開文件 if (! fs) cout”Cannot open file test””\n”。 else { (4, ios::beg)。 (?X?)。 char contents[10]。 (0, ios::beg)。 (contents,10)。 coutcontents。 } } 43 應(yīng) 用 舉 例 例 重載運算符 “ ”和 “ ”, 使用戶能直接輸入和輸出復(fù)數(shù) 。 # include class plex{ double real,imag。 public: plex(double r, double i) { real=r。 imag=i。} plex( ) { real=0。 imag=0。} friend plex operator+(plex, plex)。 friend ostream amp。operator(ostreamamp。, plexamp。)。 friend istream amp。operator(istreamamp。, plexamp。)。 }。 plex operator+(plex a, plex b)//定義重載運算符 “ +” { plex temp。 =+。 =+。 return temp。 } 44 接 1 例 ostream amp。operator(ostream amp。output, plex amp。obj) //定義重載運算符“ ” { outputobj,real。 if(0) output”+”。 if(!=0) output“i”。 return output。 } istream amp。operator(istream amp。input, plex amp。obj) //定義重載運算符“ ” { cout”Input the real, imag of the plex:\n”。 input。 input。 return input。 } 45 void main() { plex c1(,),c2,c3。 cout”the value of c1 is:”c1endl。 cinc2。 cout”the value of c2 is:”c2endl。 c3=c1+c2。 cout”the value of c3 is:”c3endl。 } 程序運行后屏幕顯示為: the value of c1 is: + Input the real, imag of the plelx: 若此時輸入: 屏幕上又顯示出: the value of c2 is + the value of c3 is + 46 接 2 例 例 下面的程序建立了類 triangle,用來存儲直角三角形的寬與高。這個類的重載輸出運算符函數(shù)在屏幕上顯示三角形。 include class triangle{ int height,base。 public: triangle (int h, int b) { height=h。 base=b。 } friend ostream amp。operator (ostream amp。stream, triangle ob)。 }。 ostream amp。operator(ostream amp。stream, triangle ob) { int I,j,h,k。 i=j=。 for (h=。h。h) { for (k=i。k。k) stream? ?。 stream?*?。 47 接 1 例 if (j!=i) { for(k=ji1。k。k) stream? ?。 stream?*?。 } i。 streamendl。 } for (k=0。k。k++) stream?*?。 streamendl。 return stream。 } void main () { triangle t1(5,5),t2(10,10),t3(12,12)。 coutt1endl。 coutt2endl。 coutt3endl。 } 48
點擊復(fù)制文檔內(nèi)容
數(shù)學(xué)相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖鄂ICP備17016276號-1