1

"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;
}

file used in code

hadi khan
  • 41
  • 5
  • You can use std::string back() to access the last char and check for newline, if that is what you were after. – kvr Nov 12 '22 at 20:51
  • @kvr i have to detect end of the line in the file where the keywords of any URL ends because after the keywords there is an empty line and then the next URL starts i have to stop whenever the line containg keywords ends – hadi khan Nov 12 '22 at 20:55
  • 2
    test the return value of getline – Neil Butterworth Nov 12 '22 at 20:57
  • 1
    `while (!myfile.eof())` is a bug waiting to happen. You're also susceptible to issues from mixing `getline` and `>>`. Read the file line by line and if you want to read individual words from a line use a `stringstream`. – Retired Ninja Nov 12 '22 at 21:02
  • 2
    [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) and [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) are worth reading. – Retired Ninja Nov 12 '22 at 21:03
  • @RetiredNinja from what you are saying is i should store the whole lines of keywords in a string array then use stringstream to copy all keywords in another string array one array contains the whole line and another array for attaing each keyword from that string am i right? – hadi khan Nov 12 '22 at 21:09
  • 1
    I would read a line at a time. If the line is empty or it contains "http://" skip it. If not `std::stringstream ss(line); while (ss >> word) { /* do something with the word */ }` This would be much simpler if you used a `std::vector` instead of a huge array, or if you don't want duplicate keywords a `std::set` or `std::unordered_set` to store the keywords. – Retired Ninja Nov 12 '22 at 21:20

0 Answers0