Why don't you let the user input the search term instead of hard coding it ?
Also it would be much more efficient if you read and store the file in a string and only search the string.
Adding to Thomas1965,
You could try this pseudocode,
1. Put all strings to be searched in a container, i would use std::vector<std::string> searches;
2. Read file content as suggested by Thomas1965 (string content = read_file("your filename");)
3. Make a variable, count, to keep track of how many times you have taken something from searches
4. Take a string from searches (searching)
4.1. Increment count by 1
5. Start search from pos 0 of read file(content)
5.1 If the string (searching) is found, say string is found otherwise say string is not found
5.2 Set iterator of content to the beginning (i = 0);
6. Repeat 4 to 5.2 until count = searches.size();
5.1 can be modified: if found, remove searching from searches. In such a way, you could print out strings that were not found. Or, found strings could be put in a different containter say std::vector<std::string> foundStrings;
I hope my thoughts would be helpful. Try that lets see.
/**
* @file readSearchString.cpp.
* @date 2018-06-06
* @author blongho
* @brief Reads contents from a file and does search to see if content contains certain values
*/
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
std::string read_file(constchar *filename); // read content
bool stringIsFound(const std::string & searching, const std::string & content);
int main()
{
//std::string readFile = read_file("your file name");
// sample read file
std::string readFile{"\tThe greatest scientist that ever existed is not known.\n\t""Some people call him God, others call him Allah. Among those that call\n\t""him God, some even think that he is Yahweh, Messaih, and a lot more.\n\t""If you gave him this same problem he would have said search for this\n\t""in this and that will happen, just as he said \"Let there be light and""there was light\".\n"
};
std::cout << readFile << std::endl;
std::vector<std::string> searches{};
searches.push_back("god"); // note this is not same as God.
searches.push_back("Allah");
searches.push_back("people");
searches.push_back("Einstein");
searches.push_back("Moobman");
std::vector<std::string> foundStrings{};
std::vector<std::string> notFoundStrings{};
for (auto &search : searches){
if (stringIsFound(search, readFile)){
std::cout << "\t" << search << " is found" << std::endl;
// process found string
//foundStrings.push_back(search);
}
else{
std::cout << "\t" << search << " is not found" << std::endl;
// process not found string here or later
//notFoundStrings.push_back(search);
}
}
std::cout << std::endl;
return 0;
}
/**
* @fn bool stringIsFound(const std::string & searching, const std::string & content)
*
* @brief String is found
* @param searching The searching string.
* @param content The content.
* @return True if it succeeds, false if it fails.
*/
bool stringIsFound(const std::string & searching, const std::string & content)
{
return content.find(searching) !=std::string::npos;
}
/**
* @fn std::string read_file(const char *filename)
* @brief please note that error checking is omitted
* @param filename Filename of the file.
* @return The file as a string.
*/
std::string read_file(constchar *filename)
{
std::stringstream buffer;
buffer << std::ifstream(filename).rdbuf();
return buffer.str();
}