我正在创建一个程序,它将打开一个文件并在文本中搜索所需的单词。我创建了以下词库...
Lawyer
Smith Janes
Doctor
Michael Zane
Teacher
Maria Omaha
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <sstream>
using namespace std;
int main ()
{
// Declarations
string reply;
string inputFileName;
ifstream inputFile;
char character;
cout << "Input file name: ";
getline(cin, inputFileName);
// Open the input file.
inputFile.open(inputFileName.c_str());
// Check the file opened successfully.
if ( ! inputFile.is_open())
{
cout << "Unable to open input file." << endl;
cout << "Press enter to continue...";
getline(cin, reply);
return 1;
}
现在我将整个文件保存到一个字符串中,我如何在该字符串中搜索我正在寻找的特定单词......
我正在从这个网站http://www.cprogramming.com/tutorial/lesson10.html学习 C++
我想你用过 string::find
,但我找不到太多关于如何在这个网站旁边搜索的参考资料。
http://www.cplusplus.com/reference/string/string/find/
此部分将显示整个文件。
string original;
getline(inputFile, original, '\0');
cout << original << endl;
cout << "\nEnd of file reached\n" << endl;
// Close the input file stream
inputFile.close();
cout << "Press enter to continue...";
return 0;
}
这就是我认为程序应该采取的行动......
Please enter a word: Smith Janes
Smith Janes Lawyer
another example....
Please enter a word: Doctor
Michael Zane Doctor