2 Replies - 1014 Views - Last Post: 11 December 2013 - 02:28 AM Rate Topic: -----

#1 Michael_Scott  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 19-November 13

How to search for words in text file?

Posted 11 December 2013 - 01:54 AM

I am having a lot of trouble with this search program. My professor gave us an assigment to read text file and search for certain words in it. I tried getting it to read the words from the file, but it just outputs gibberish. I tried using the strcmp and strstr operators, but every time it just reads the first few words from the file. Here's a copy of his assignment:

“Write a C++ program to search a text file for occurrences of any of the following trigger words: "KALE", "ARTISANAL", "FIXIE", "KEROUAC", "JARMUSCH", "NERDCORE", "PBR", "TATTOO", "SCARF", "INDIE", "TURNTABLE". An "occurrence" means that the word in the file exactly matches the trigger, or the trigger appears as a substring of the word in the file. Your program will convert each word to uppercase before scanning it for an occurrence.

Your program will prompt the user for a filename, then will open that file and search for occurrences of the trigger words. Each time the program finds an occurrence, the program will print a line of text with the filename, the trigger word found, and the line number where it was found. There can be zero or more occurrences in the file; all will be reported. If no occurrence is found, the program will print "No Occurrences found". Use the file here and others. Your code should work for ANY filename supplied by the user. ”

I tried rearranging these string library functions, but still nothing. Either an error or just reading the first few lines. Or seeing nothing at all. After the user types in the filename, I call the function wordSearch with the name of the file as the argument. I have a two-dimensional array with one size, SIZE, which is 11, the number of words, and another size, MAX, which 13, which is the maximum number that I want the program to read up to so that the program does not go past the array's boundaries. (I probably don't need this though). In lines 46-73, I am trying to have the program step through the array of words while reading the file. The idea is that I want the program to read each word in the file until it reaches the end of the file, and if a word in the file matches a word in the array then I want it to display the word, where it’s been found, and how many times it was found. Right now I just want to start off with reading the text file and comparing words. Here's my code below:

#include <iostream>
#include <cstring>
#include <fstream>
#include <string>

using namespace std;
bool wordSearch(ifstream &);

int main()
{
	string fileName;	
	bool wordFound;
	ifstream file;
	cout << "Enter a file name\n";
	getline(cin, fileName);
	int count = 0;

	file.open(fileName);
	if (!file)
	{
		cout << "Dang. The file was not found." << endl;
		return 1;
	}
wordFound = wordSearch(file);
if (wordFound)
	cout << "The words have been found" << endl;
else
{
	cout << "Not found" << endl;
}
return 0;
}	
	
bool wordSearch(ifstream &hipsters)
{
	int index;
	int position = -1;
	const int SIZE = 11;
	const int MAX = 13;
	string fileWords;
	char words[SIZE][MAX] = {"kale", "artisanal", "fixie", "kerouac", "jarmusch", "nerdcore", "pbr", "tattoo", "scarf", "indie", "turntable"};
	char *strPtr = NULL;
	bool found = false;
	

	while(!hipsters.eof())
	{
		for (index = 0; index < SIZE; index++) //Loop that steps through each word	
		words[index];
		{
			for (index = 0; index < SIZE; index++) //loop that checks to see if each word can be found in the file
			strstr(hipsters, words); //strstr function searches for strings even if they are hidden in other strings
			found = true;
			}		
			return 0;
		
	}
	return found;
}

// for all words infile
// for all trigger words
// if trigger or substring
// print message


The errors I've been getting are as follows:

1>c:\users\mine\desktop\fall qtr 2013 notes and hw\cs 2360\file search\file search\file search.cpp(52): error C2665: 'strstr' : none of the 2 overloads could convert all the argument types
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(196): could be 'const char *strstr(const char *,const char *)'
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\string.h(220): or 'char *strstr(char *,const char *)'
1> while trying to match the argument list '(std::ifstream, char [11][13])'

I would appreciate any help with this to get me started.

Is This A Good Question/Topic? 0
  • +

Replies To: How to search for words in text file?

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: How to search for words in text file?

Posted 11 December 2013 - 02:18 AM

Michael: Please don't start multiple threads for the same topic. Confusion and duplicate posts will just pour out like crazy.

I have just posted an answer to your earlier post on this subject.
Was This Post Helpful? 0
  • +
  • -

#3 Michael_Scott  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 13
  • Joined: 19-November 13

Re: How to search for words in text file?

Posted 11 December 2013 - 02:28 AM

I didn't realized I posted the thread twice. I clicked post, pressed the stop button, and clicked again. My apologies.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1