Compare lines in the same text file

Hi, guys!
I would like to read a .txt file (of maaany lines) and compare each line with all the rest, in order to check if some of them are repeated.
Any ideas how I should do it?

Thanks in advance
Hi Sophie,

if you did read the text file into a list listOfTextLines the algorithm to examine if there is a repeated line could look like this: (pseudo code)

1
2
3
4
5
6
7
8
    bool atLeastOneTextLineIsRepeated = false;
    for(int i=0; i<listOfTextLines.size(); i++)
    {
         for(int j=i+1; j<listOfTextLines.size(); j++)
        {
            atLeastOneTextLineIsRepeated |= ( listOfTextLines.at( i ) ==  listOfTextLines.at( j ) );
        }
    }


Important is that

 
 for(int j=i+1; ...


in the inner loop, because you don't want to compare a line with itself.
Last edited on
read every line as a string with getline() and put it into a vector. then sort the vector so that if there any identical lines they are one after the other.
Hi, guys, thanks!
I am having problem assigning the lines to a vector components. I can read them with getline(), print them out e.t.c., but I can't find a way to store them..
1
2
3
4
5
std::fstream file;
std::vector< std::string > vec;
std::string str;
std::getline(file, str);
vec.push_back(str);
Great!!!
Thanks!!!
Topic archived. No new replies allowed.