13 Replies - 853 Views - Last Post: 22 February 2010 - 05:34 PM Rate Topic: -----

#1 Marson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 22-February 10

need help searching a file for specific keyword c++

Posted 22 February 2010 - 03:12 PM

Hey guys, I am trying to write a program that will use command line arguments to detect user inputed keywords in a text file and output how many times the keyword/s appear. The command line should read like this...

programname -f inputfile keyword1 keyword2...keywordN

The problem I have is I do not know how to search the file for a specific string. For instance, if the keyword to search is "the", I do not know how to make the program read the file and search specifically for the string "the." If anyone can help me out, it would be really appreciated, this is killing me. Here is my code so far:

int main(int argc, char *argv[])
{
   int i;

   FILE * fptr;
   fptr = fopen("myfile.txt","r");

   if(fptr == NULL)
  {
    cout << "file does not exist." << "\n";
  }

   for (i = 1; i < argc; i++)
   {
	   if (strcmp(argv[i], "-f") == 0)
	   {

This post has been edited by JackOfAllTrades: 22 February 2010 - 03:27 PM
Reason for edit:: Added code tags. PLEASE!!! [code]...PUT YOUR CODE IN HERE...[/code]


Is This A Good Question/Topic? 0
  • +

Replies To: need help searching a file for specific keyword c++

#2 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 03:19 PM

/*your string name*/.compare() == /*string*/
count++



http://cplusplus.com...string/compare/

This post has been edited by IngeniousHax: 22 February 2010 - 03:20 PM

Was This Post Helpful? 0
  • +
  • -

#3 Marson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 22-February 10

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 03:24 PM

Im a little confused by what you posted, Is it supposed to be /*your file name*/.compare() == /*string*/?

I don't see how it is searching the file for a keyword
Was This Post Helpful? 0
  • +
  • -

#4 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 03:58 PM

You should open the file, read it into a buffer, loop through the buffer and compare it to the user entered string.
Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 03:59 PM

IngeniousHax's post assumes you already are reading the file.

To read the file have a look at file processing in c++

Input/Output with files
Was This Post Helpful? 0
  • +
  • -

#6 Marson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 22-February 10

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 04:02 PM

Thank you, that sounds like a great way to do it! I will work on it and post what I can come up with.

Also, on another forum someone said I was using c not c++. The class I am in is a mixture, will it affect my code if I use things from both when dealing with files? I think so far I have been manipulating them with c.
Was This Post Helpful? 0
  • +
  • -

#7 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 04:10 PM

looks like C++ to me. it will be fine either way.

are you going to call this program grep?
Was This Post Helpful? 0
  • +
  • -

#8 Marson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 22-February 10

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 04:14 PM

View Postcarltech, on 22 February 2010 - 03:10 PM, said:

looks like C++ to me. it will be fine either way.

are you going to call this program grep?



I'm not sure what grep is. We have not discussed it I guess, would it be useful?
Was This Post Helpful? 0
  • +
  • -

#9 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 04:23 PM

No, not really. Granted with c++ you can use fstream. and use something (been awhile since I used files infile.open (where i think infile is th name of your file to open...


infile = "file.txt";
infile.open



I am probably completely wrong, but...
Was This Post Helpful? 0
  • +
  • -

#10 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 04:30 PM

Either way it is possible is all I meant.

don't you mean infile.open(); lol

and grep is a linux command used to look things up in files. hence the joke.

This post has been edited by carltech: 22 February 2010 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

#11 Marson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 22-February 10

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 04:46 PM

Ok here is where I'm at. Im kind of stumped on how to loop through the buffer and compare. Any ideas?

int main(int argc, char *argv[])
{
   long size;
   int i, result;
   char * buffer;
   

   FILE * fptr;
   fptr = fopen("myfile.txt","r");

   if(fptr == NULL)
  {
    cout << "file does not exist." << "\n";
  }

   fseek(fptr, 0, SEEK_END);
   size = ftell(fptr);



   buffer = (char*) malloc (sizeof(char)*size);
  if (buffer == NULL) 
  {
	  cout << "There is a memory error";
  }

  result = fread (buffer,1,size,fptr);
  if (result != size) 
  {
	  cout << "There is a reading error";
  }
  
   
   for (i = 1; i < argc; i++)
   {
	   if (strcmp(argv[i], "-f") == 0)
	   {











	return 0;
}

Was This Post Helpful? 0
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 05:04 PM

The C++ iostream extract operator, ">>", can extract individually the words of a text file, one at a time.

From an example (modified slightly) in the doc page I gave you before:

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
//  string line;
  string word;

  ifstream myfile ("example.txt");

  if (myfile.is_open())
  {
    while (! myfile.eof() ) // should be  "while(myfile) {"
    {
//    getline (myfile,line); this reads an entire line of text.

      myfile >> word; // extract one word at a time

      cout << word << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;


Was This Post Helpful? 0
  • +
  • -

#13 Marson  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 23
  • Joined: 22-February 10

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 05:12 PM

That is interesting, but when you extract each word individually where are they stored? I need to be able to compare it to the keywords entered on the command line, and I'm not real sure how to do this.

disregard that last post, I misread. I see that it stores it to word, but how would I got about comparing each individual word to the inputed keywords?
Was This Post Helpful? 0
  • +
  • -

#14 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: need help searching a file for specific keyword c++

Posted 22 February 2010 - 05:34 PM

Create an array of the command line words.
Doh!

The command line words are already in an array - argv[]. So set the for() loop to use the first word's argc index value:

for (int i = 1; i < argc; i++) {
	if (word.compare(argv[i]) == 0)
		cout << argv[i] << " ";
}



See the string library String class

This post has been edited by n8wxs: 22 February 2010 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1