gpt4 book ai didi

c++ - 如何回到文本文件的第一个

转载 作者:行者123 更新时间:2023-11-27 22:30:51 26 4
gpt4 key购买 nike

我有一个 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() ) ...我的代码有什么问题?

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

最佳答案

调用:

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

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

关于c++ - 如何回到文本文件的第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2705141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com