Problem with using find()

Okay, so basically, I'm trying to write a basic program that will search a large text file for a word, and return it afterwards.
Here's the code I have so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  #include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

int main(){
	string movieName='Zonde';
	string fileName="actors.2010.list";
	ifstream fileHandle;
        
    fileHandle.open(fileName.c_str());
    string tempString;
    size_t found=0;
    int position=0;
    if (fileHandle.good()){
        while(found!=string::npos){
        fileHandle.seekg(position);
        getline(fileHandle, tempString, '\n');
        found=tempString.find(movieName, position);
        position = fileHandle.tellg();
        cout<<tempString<<endl;
        }
        fileHandle.close();}              
	cout << position;
	cout <<tempString;
	system("PAUSE");
	return 0;
}


(This is obviously part of a larger project, hence the extra variables. I'm just testing some of the algorithms out.)
Basically, what I want the program to do, is go line-by-line through the text file until it finds a match. The word I am searching for is a bit down from the top, however, the program always returns the first line. I can't quite see the problem with what I'm doing though. Since the text file is large, I'll just copy some of what it contains...
$hort, Too
Ghetto Physics (2010)

$lim, Bee Moe
"Money Power & Respect: The Series" (2010) {Bottom Dollar (#2.7)} [Bee Moe Slim]
"Money Power & Respect: The Series" (2010) {Flip Side (#2.5)} [Bee Moe Slim]

'Bootsy' Thomas, George
My Song for You (2010) [Cooley's Customer/Celebration Guest] <16>

'Little Kato', Chris
Jackass 3D (2010) [Himself] <68>

'Monkey' Stevens, Neal
Half Hearted (2010) (as Monkey) [Tattooist] <15>

's Gravemade, Nienke
Criss Cross (2010) [Annie]

't Hoen, Daniƫl
Zonde (2010) [Yorgos Znapoudos] <2>

-, Manolo
Counting My Days (2010)


As you can see, Zonde is pretty far down the list, yet the $hort, Too is always returned. Anyone care to offer a hand? Is the problem with my use of find()? Or with the ifstream?

I have also tried initiating the loop with
while(movieName.find(tempString)!=string::npos)
to no success. It appears that the problem is obviously somehow the loop condition, since it's satisfied before it is supposed to be.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    const string movie = "Zonde" ; // use "

    const string fileName = "actors.2010.list"; // const added

    ifstream file( fileName.c_str() ) ; // the constructor opens the file

    string line;
    int line_number = 0 ;

    while( std::getline( file, line ) ) // canonical: for each line in the file
    {
        ++line_number ;

        auto pos = line.find(movie) ; // search for movie in the line just read

        if( pos != std::string::npos ) // found it!
        {
            std::cout << "found '" << movie << "' in line " << line_number
                      << " at position " << pos << '\n' ;
        }
    }
}
It appears I had overengineered it, thank you!
Topic archived. No new replies allowed.