64,661
社区成员
发帖
与我相关
我的任务
分享
string str_fn="c:\\a.txt";
ifstream ifs(str_fn.c_str());
string str_line="";
streampos t_sp;
t_sp=ifs.tellg();
while(getline(ifs,str_line))
{
if(check(str_line)==true)
{
ifs.seekg(t_sp);
break;
}
t_sp=ifs.tellg();
//cout<<"streampos="<<t_sp<<endl;
}
string str_fn="c:\\a.txt";
ifstream ifs(str_fn.c_str());
string str_line="";
streampos t_sp;
t_sp=ifs.tellg();
while(getline(ifs,str_line))
{
if(check(str_line)==true)
{
ifs.seekg(t_sp,ios::beg);//这样试试
break;
}
t_sp=ifs.tellg();
//cout<<"streampos="<<t_sp<<endl;
}
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
bool check(string str)
{
if(str[0] == '3') return true;
return false;
}
int main()
{
string str_fn="c:\\a.txt";
ifstream ifs(str_fn.c_str());
string str_line="";
streampos t_sp;
t_sp=ifs.tellg();
while(getline(ifs,str_line))
{
if(check(str_line)==true)
{
ifs.seekg(t_sp);
break;
}
t_sp=ifs.tellg();
cout<<"streampos="<<t_sp<<endl;
}
ifs>>str_line;
cout<<str_line;
}
----------------------------
文件:
1:hello
2:world
3:ff
5:fds
4:fdiw
----------------------------
输出:
streampos=9
streampos=18
3:ffPress any key to continue
--------------------------
结论:
没问题啊!!
string line;
ifstream input("a.txt",ios::in);
while(1){
getline(input,line);
if(input.eof())
break;
if(check(line)==true){
input.seekg(0,ios::beg);
break;
}
}
input.close();