Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bioagentX

Finding words in files

This topic is 5340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to find the amount of times a certain word occurs in a file. For example, there is a text file kennedy.txt which has the following contents: Ask not, what your country can do for you; Ask what you can do for your country! John F. Kennedy, Inaugural Speech, 1961 I''m trying to make a program that writes how many times the word "you" appears. My code so far however stops searching each line of the file after it finds the first occurance of the word "you" in that line. In other words, I''m getting that the word "you" occurs only twice when it in fact occurs four times. Here is my code:

int main()
{
	ifstream inFile("kennedy.txt", ios::nocreate);

	if(inFile.fail())
	{
		cout<<"File could not be opened";
		return 0;
	}
	else
	{
		String S;
		int lineNum = 0;
		int charNum = 0;
		int uAmt = 0;
		


		while(getline(inFile, S))
		{
			cout<<">"<<S<<endl;
			lineNum++;
			charNum+= S.length();

		       if(S.find()==npos)
                          ;
                       else
                       {
                             uAmt++;
                       }


		}

		cout<<"There are "<<lineNum<<" lines."<<endl;
		cout<<"There are " <<charNum<<" characters."<<endl;
		cout<<"The world \"you\" occurs "<<uAmt<<" times.";

		cout<<"Done!";


	}



	return 0;

}

Share this post


Link to post
Share on other sites
Advertisement
Try instead of if find then else

pos = 0;
while ((pos = S.find("word", pos)) != npos)
increase counter

Does it help?

(A little pseudo-codish perhaps, but you get the idea I hope)

[edited by - nystagmus on March 6, 2004 11:43:32 AM]

Share this post


Link to post
Share on other sites
Actually, that doesn''t work unfortuanetely. It is due to the fact that the find function does not take two parameters. Any other ideas?

Share this post


Link to post
Share on other sites
quote:
Original post by bioagentX
Actually, that doesn''t work unfortuanetely. It is due to the fact that the find function does not take two parameters. Any other ideas?



It would work if your compiler was c++ standard compliant.

Share this post


Link to post
Share on other sites
quote:
Original post by yspotua
It would work if your compiler was c++ standard compliant.



Or if you used a standard library.

Share this post


Link to post
Share on other sites
That''s true,
however, I have to use the library that the authors of the book made in order to use their example code. This is a c++ textbook that I''m learning c++ from so that I can be extra prepared for java next year, as well as for my own personal interest in learning c++.

Share this post


Link to post
Share on other sites
That just means that you have to create this functionality yourself. This is not necessarily a bad thing since the point seems to be to learn C++.

find simply finds the position of the first occurrence of the string you specify. What you need to do is to create a new string then for each search, where you remove the part that you have already searched.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!