如何回到第一个文本文件

Nae*_*ard 0 c++ readfile

我有一个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;
    }
}
Run Code Online (Sandbox Code Playgroud)

第一次一切都好,但在第二次运行时它不会进入第二次(而(!myfile.eof())...我的代码出了什么问题?

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

Ale*_*lli 5

呼叫:

myfile.seekg(0, ios::beg);
Run Code Online (Sandbox Code Playgroud)

将文件读指针设置回开头.