猿问

VC++6.0下关于file.getline()的一个死循环。

代码如下://vc++6.0下运行。

#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中的小写字母变为大写,写入TextFile。

          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的内容,无i时会死循环

        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 .

运行后:

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 ..

*/

未来的开拓者
浏览 1606回答 0
0回答
随时随地看视频慕课网APP
我要回答