0

我有一个 txt 文件,其中有一些行......我的代码是:

string line;
ifstream myfile("c:\\main.txt");

bool passport = true;

while(passport==true){

    int pos1=0;
    cout<<setw(20)<<"PassPort_Number : ";
    cin>>add.Id ;
    if (myfile.is_open())
    {
        while(!myfile.eof()){
            getline(myfile,line);
            pos1+=line.find(add.Id);
            cout<<pos1;
        }
    }

    if(pos1<0)
        passport=false;
    else {
        cout<<"PassPort Number tekrariye :d"<<endl;
    }
}

第一次一切正常,但在第二次运行时它没有进入第二次 while (while(!myfile.eof() ) ...我的代码有什么问题?

当它到达文本文件的末尾时,它不会在下一个循环中回到文件的第一个……我怎样才能回到文本文件的第一个?

4

1 回答 1

5

称呼:

myfile.seekg(0, ios::beg);

将文件读取指针设置回开头。

于 2010-04-24T16:13:22.970 回答