Hello,
I'm working on a code for my project at college. The goal of the project is to find and extract keywords, and the sentences, which contain these keywords from many text files, which I have already downloaded from Internet using another code. These text files are actually source codes of different websites and my program needs to search for certain keywords, which I store in another text file called "keywords.txt". It should also search for all keywords in all text files. So I tried to do it using some while-loops. Although I got some results, my code only searchs for the first keywords, that lies on the top line of "keywords.txt" and other keywords are unfortunately not searched. I can search only this one keywords in all text files; I get the places and names of these text files from a file called "addresses.txt". Could you please look at my code and tell me what could be wrong about it and what should I do for my code to search for all keywords in "keywords.txt"?
I would also appreciate some hints about how I could manage this process using vector class, since using arrays is not really appropriate, because I have to change the size of arrays everytime when I add new addresses or keywords, manually. I have some knowledge of vectors, but I couldn't implement it into my code. Here is the code I have written:
--------------------------------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <sstream> // string stream class'ı
#include <vector>
using namespace std;
void Search_Keyword(void)
{
int j = 0;
int i = 0;
size_t position = 0;
string line;
string keyword;
string keyword_Array[10];
string name_Array[13];
string link_Array[13];
string link;
string name;
ifstream addresses("addresses.txt");
ifstream keywords("keywords.txt");
keyword = "";
while(keywords>>keyword)
{
keyword_Array[j] = keyword;
string search_Str = keyword_Array[j];
while(addresses>>link>>name)
{
name_Array[i] = "URLS/";
name_Array[i] += name;
name_Array[i] += ".txt";
link_Array[i] = link;
ifstream url_txt(name_Array[i].c_str());
while(getline(url_txt,line))
{
string word;
istringstream myObj(line);
while(myObj>>word)
{
if ((word == search_Str))
{
cout<<"found "<<search_Str<<" in "<<name_Array[i]<<endl;
}
}
}
url_txt.close();
if(i < 12)
i++;
}
j++;
}
addresses.close();
keywords.close();
}
int main()
{
Search_Keyword();
return 0;
}
--------------------------------------------------------------------------------
Thanks for your help :)