代碼如下://vc++6.0下運(yùn)行。#include <iostream>#include <fstream>#include <stdlib.h>using namespace std;void main(){??? char ch;????????char sten[81]; ????fstream infile;????infile.open("File.txt",ios::in); ????if(infile.fail()){ cout<<"infile error!"; exit(1); } ????fstream outfile("TextFile.txt",ios::out|ios::in);????if(outfile.fail()){ cout<<"outfile error!"; exit(2); } ????while(!infile.eof()){ ? //把File中的小寫字母變?yōu)榇髮懀瑢懭隩extFile。????????? infile.get(ch); ch=toupper(ch); outfile<<ch; ??? }
??????????????? infile.close();??? outfile.flush(); outfile.clear(); ??? outfile.seekg(0,ios::beg); ??? outfile.seekp(0,ios::beg); ??? int i=1;??? while(!outfile.eof()&&i<10){ ?//顯示TestFile的內(nèi)容,無(wú)i時(shí)會(huì)死循環(huán) ??????? outfile.getline(sten,81);????????cout<<i<<sten<<endl;??????? i++;??? } ??? outfile.close();}/*文件File.txt:When Day Is DoneIf the day is done ,If birds sing no more .If the wind has fiagged tired ,Then draw the veil of darkness thick upon me ,Even as thou hast wrapt the earth with The coverlet of sleep and tenderly closed ,The petals of the drooping lotus at dusk.From the traverer,Whose sack of provisions is empty before the voyage is ended ,Whose garment is torn and dust-laden ,Whose strength is exhausted,remove shame and poverty ,And renew his life like a flower underThe cover of thy kindly night .運(yùn)行后:TextFile.txt:WHEN DAY IS DONEIF THE DAY IS DONE ,IF BIRDS SING NO MORE .IF THE WIND HAS FIAGGED TIRED ,THEN DRAW THE VEIL OF DARKNESS THICK UPON ME ,EVEN AS THOU HAST WRAPT THE EARTH WITH THE COVERLET OF SLEEP AND TENDERLY CLOSED ,THE PETALS OF THE DROOPING LOTUS AT DUSK.FROM THE TRAVERER,WHOSE SACK OF PROVISIONS IS EMPTY BEFORE THE VOYAGE IS ENDED ,WHOSE GARMENT IS TORN AND DUST-LADEN ,WHOSE STRENGTH IS EXHAUSTED,REMOVE SHAME AND POVERTY ,AND RENEW HIS LIFE LIKE A FLOWER UNDERTHE COVER OF THY KINDLY NIGHT ..*/
VC++6.0下關(guān)于file.getline()的一個(gè)死循環(huán)。
未來(lái)的開拓者
2017-04-24 13:14:28