合工大計(jì)算機(jī)學(xué)院 程序設(shè)計(jì) 10第十章 C++的輸入輸出.ppt
第十章C+的輸入輸出,流文件文件流的使用:文件的輸入和輸出,10.1流與文件一、流,C+語(yǔ)言提供了流用于讀寫(xiě)文件。接收輸出數(shù)據(jù)的地方稱(chēng)為目標(biāo),發(fā)出輸入數(shù)據(jù)的地方稱(chēng)為源。輸入輸出操作可以看作是字符序列在源、目標(biāo)與對(duì)象之間的流動(dòng),因而將完成輸入輸出操作的類(lèi)稱(chēng)為流類(lèi)。源流目標(biāo)流輸入緩沖區(qū)(讀)輸出緩沖區(qū)(寫(xiě))流的提取運(yùn)算符>>流的插入運(yùn)算符<>文本文件的寫(xiě)操作是向流中寫(xiě)一個(gè)字符流的插入運(yùn)算符<<,例1:將文件file_from拷貝到文件file_to中去,#include#includeintCopyFile(char*file_from,char*file_to)/file_from:輸入文件名(源文件);file_to:輸出文件名(目標(biāo)文件)charch;/文件不存在則打開(kāi)失敗ifstreamfrom(file_from,ios:in|ios:nocreate);/以文本方式打開(kāi)if(!from)cout<<"Cannotopen"<<file_from<<"forinput!n"return0;/返回0表示復(fù)制文件不成功ofstreamto(file_to);/文件不存在則創(chuàng)建新文件if(!to)cout<<"Cannotopen"<<file_to<<"foroutput!n"return0;/從流from中讀一個(gè)字符到ch中,成功的話則寫(xiě)入to,否則文/件的每個(gè)字符都讀完了while(from.get(ch)to.put(ch);from.close();to.close();/關(guān)閉文件return1;/返回1表示文件復(fù)制成功,例2:把一個(gè)整數(shù)、一個(gè)浮點(diǎn)數(shù)和一個(gè)字符串寫(xiě)到名為out.txt的文本文件中,#includeintmain()ofstreamoutfile;outfile.open("out.txt");if(!outfile)cout<<"Cannotopenout.txtforoutput!n"return0;/返回0表示打開(kāi)文件不成功floatf=123.23;intn=10;outfile<<n<<""<<f<<"n"outfile<n>>f>>ch>>str;cout<<n<<""<<f<<""<<ch<<"n"cout<>運(yùn)算符號(hào)跳過(guò)空白符號(hào)(如空格,回車(chē)),然后讀入對(duì)應(yīng)于輸入對(duì)象類(lèi)型的字符,例4:百錢(qián)買(mǎi)百雞,#include#includevoidmain()inti,j,k;chara28;ofstreamofile("d:myfile.txt");/打開(kāi)文件ofile<<"公雞母雞小雞"<<endl;for(i=0;i<=20;i+)for(j=0;j>,它不能讀白字符if(ai=n)break;i+;/把文件中的第一行(到n)內(nèi)容讀入a字符數(shù)組ai=0;/a加上結(jié)束符,成為完整的字符串cout>i>>j>>k;/由文件讀入if(ifile.eof()!=0)break;cout<<setw(6)<<i<<setw(10)<<j<<setw(10)<<k<<endl;/屏幕顯示ifile.close();,myfile.txt,公雞母雞小雞02575418788118112484,例5,#include/包含#include#includeclassinventory/產(chǎn)品目錄charDescription20;charNo10;intQuantity;doubleCost;doubleRetail;/零售價(jià)public:inventory(char*="#",char*="0",int=0,double=0,double=0);voiddisplay();Bdatatofile(ofstream,inventory:inventory(char*des,char*no,intquan,doublecost,doubleret)strcpy(Description,des);strcpy(No,no);Quantity=quan;Cost=cost;Retail=ret;voidinventory:display()cout.setf(ios:left);cout<<setw(20)<<Description<<setw(10)<<No;cout.unsetf(ios:left);/要改為右對(duì)齊,先清左對(duì)齊cout.setf(ios:right);cout<<setw(10)<<Quantity<<setw(10)<<Cost<<setw(10)<<Retail<<endl;,inventory:Bdatatofile(ofstream/由此可見(jiàn)讀和寫(xiě)是完全對(duì)稱(chēng)的過(guò)程,次序決不能錯(cuò),voidmain()inventorycar1("夏利2000","805637928",156,80000,105000),car2;inventorymotor1("金城125","93612575",302,10000,13000),motor2;ofstreamddatafile("d:Ex9_10.data",ios:out|ios:binary);car1.Bdatatofile(ddatafile);motor1.Bdatatofile(ddatafile);cout<<"對(duì)象car1:"<<endl;car1.display();cout<<"對(duì)象motor1:"<<endl;motor1.display();cout<<"對(duì)象car2:"<<endl;car2.display();cout<<"對(duì)象motor2:"<<endl;motor2.display();ddatafile.close();ifstreamsdatafile("d:Ex9_10.data",ios:in|ios:binary);/重新打開(kāi)文件,從頭讀取數(shù)據(jù)car2.Bdatafromfile(sdatafile);/從文件讀取數(shù)據(jù)拷貝到對(duì)象car2if(sdatafile.eof()=0)cout<<"讀文件成功"<<endl;cout<<"對(duì)象car2:"<<endl;car2.display();motor2.Bdatafromfile(sdatafile);/繼續(xù)從文件讀取數(shù)據(jù)拷貝到對(duì)象motor2if(sdatafile.eof()=0)cout<<"讀文件成功"<<endl;cout<<"對(duì)象motor2:"<<endl;motor2.display();sdatafile.close();,