"Answered by Retired Ninja in Comments..." I am writing a code to make a search engine. I have a file with URLs and some keywords given below the URLs. There are multiple URLs with their respective keywords. I have to store all keywords (not URLs) in a string array.
I have cracked how I can skip the lines with URLs and read all lines with keywords. The problem I am facing is when I am reading the keywords word by word, I have no idea how can I detect when the line containing keywords has ended. I know if I was using char
I could have detected it with '\n'
, but in case of string
I am stuck.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
int count = 0; //for skipping URL lines
int ele=0; //for counting total keywords in the string array
string fname;
cout<<"enter file name: ";
cin>>fname;
string temp; //for skipping URL line and for temporarily storing keyword word by word
string main[10000];
fstream myfile;
myfile.open (fname);
while (!myfile.eof())
{
getline(myfile,temp); //skips lines with URLs
if(count == 2){ //after 3 lines lines with keywords come
while(temp != '\n'){ //detecting end of line
myfile>>temp; //attains single word
main[ele]=temp; // stores in main string array
ele++;
}
count = 0;
continue;
}
count++;
}
myfile.close();
for(int i=0;i<ele;i++){
cout<<main[i]<<endl;
}
system("pause");
return 0;
}