View Single Post
Join Date: Jan 2009
Posts: 7
Reputation: serhannn is an unknown quantity at this point 
Solved Threads: 0
serhannn serhannn is offline Offline
Newbie Poster

searching for keywords in multiple text files

 
0
  #1
Jan 7th, 2009
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:
--------------------------------------------------------------------------------
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream> // string stream class'ı
  5. #include <vector>
  6. using namespace std;
  7.  
  8. void Search_Keyword(void)
  9. {
  10. int j = 0;
  11. int i = 0;
  12. size_t position = 0;
  13. string line;
  14. string keyword;
  15. string keyword_Array[10];
  16. string name_Array[13];
  17. string link_Array[13];
  18. string link;
  19. string name;
  20. ifstream addresses("addresses.txt");
  21. ifstream keywords("keywords.txt");
  22. keyword = "";
  23.  
  24. while(keywords>>keyword)
  25. {
  26. keyword_Array[j] = keyword;
  27. string search_Str = keyword_Array[j];
  28.  
  29. while(addresses>>link>>name)
  30. {
  31. name_Array[i] = "URLS/";
  32. name_Array[i] += name;
  33. name_Array[i] += ".txt";
  34. link_Array[i] = link;
  35. ifstream url_txt(name_Array[i].c_str());
  36. while(getline(url_txt,line))
  37. {
  38. string word;
  39. istringstream myObj(line);
  40. while(myObj>>word)
  41. {
  42. if ((word == search_Str))
  43. {
  44. cout<<"found "<<search_Str<<" in "<<name_Array[i]<<endl;
  45. }
  46. }
  47. }
  48. url_txt.close();
  49. if(i < 12)
  50. i++;
  51. }
  52.  
  53. j++;
  54. }
  55. addresses.close();
  56. keywords.close();
  57. }
  58.  
  59. int main()
  60. {
  61. Search_Keyword();
  62. return 0;
  63. }
--------------------------------------------------------------------------------

Thanks for your help
Reply With Quote