VC编程TXT中查找字符串
#include<iostream>#include<fstream>#include<string>usingnamespacestd;voidinteractiveO...
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void interactiveOpen(ifstream & theIFStream);
int main()
{
cout << "This program searches a fiel for a given string.\n";
ifstream inStream;
interactiveOpen(inStream);
cin.ignore(255,'\n');
cout << "Enter the string being sought: ";
string name;
getline(cin,name);
cin.ignore(255,'\n');
cout << name <<endl;
string lineOfText;
for(;;)
{
getline(inStream, lineOfText);//从文件获取一行
if(inStream.eof())
break;
if(lineOfText.find(name, 0) != string::npos)
{
cout << "\n***" << name<< " "
<< "found in this file. "<<endl;
}
cout << "\nProcessing complete.\n";
inStream.close();
}
return 0;
}
程序结果应该是在txt文档中找一个字符串,如果字符串出现在第一行还能找到,但是如果字符串出现在第二行就查找不到了,不知道为什么
高手帮忙! 展开
#include <fstream>
#include <string>
using namespace std;
void interactiveOpen(ifstream & theIFStream);
int main()
{
cout << "This program searches a fiel for a given string.\n";
ifstream inStream;
interactiveOpen(inStream);
cin.ignore(255,'\n');
cout << "Enter the string being sought: ";
string name;
getline(cin,name);
cin.ignore(255,'\n');
cout << name <<endl;
string lineOfText;
for(;;)
{
getline(inStream, lineOfText);//从文件获取一行
if(inStream.eof())
break;
if(lineOfText.find(name, 0) != string::npos)
{
cout << "\n***" << name<< " "
<< "found in this file. "<<endl;
}
cout << "\nProcessing complete.\n";
inStream.close();
}
return 0;
}
程序结果应该是在txt文档中找一个字符串,如果字符串出现在第一行还能找到,但是如果字符串出现在第二行就查找不到了,不知道为什么
高手帮忙! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询