GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Dec-2007, 06:14
tootypegs tootypegs is offline
Junior Member
 
Join Date: Nov 2007
Posts: 32
tootypegs has a little shameless behaviour in the past

Finding words in files


hi, my plan is to search for particular phrases within text based documents. I know how to read in the text file but afterwards this is where the questions begin. Should i be reading the file line by line or character by chAracter?

If i declare the string "here i am", is there a particular search or find function to look for this phrase within the text document?

thanks, any help would be much appreciated!
  #2  
Old 06-Dec-2007, 07:50
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 440
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: finding words in files


The easiest way that I can think of is to load the entire text file into one string object, then look for a substring of that string that is equal to your phrase. This would be easy if you use the std::string object. Something like this:
CPP / C++ / C Code:
string phrase = "here I am";
string sfile;
// open a file for reading
while(/* not to the end of the file */)
{ // read a line
  // add it to sfile
}
// now, sfile contains the entire file
for(int i=0;i<sfile.size();i++)
{ if(phrase == sfile.substr(i,phrase.size()))
  { //the phrase has been found
  }
}
  #3  
Old 06-Dec-2007, 10:41
davis
 
Posts: n/a

Re: finding words in files


Quote:
Originally Posted by tootypegs
hi, my plan is to search for particular phrases within text based documents. I know how to read in the text file but afterwards this is where the questions begin. Should i be reading the file line by line or character by chAracter?

If i declare the string "here i am", is there a particular search or find function to look for this phrase within the text document?

thanks, any help would be much appreciated!

You can do it any way that you want, single character, line by line or the entire file (assuming that the file isn't too large). Here's an example of searching through a file for a phrase:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int length = 0;
    char* p_data;

    cout << "Enter the name of the word file: ";
    string filename;
    getline( cin, filename );
    ifstream infile( filename.c_str() );
    if( infile.is_open() && infile.good() && !infile.eof() && !infile.fail() && !infile.bad() )
    {
        infile.seekg( 0, ios_base::end );
        length = infile.tellg(); // get the file size
        infile.seekg( 0, ios_base::beg );
        p_data = new char[length];
        if( p_data )
        {
            infile.read( p_data, length );
        }
        infile.close();

        string s( p_data );
        delete [] p_data;
        p_data = 0;

        cout << "Enter the phrase you want to find in the text contained in file " << filename << ": ";
        string phrase;
        getline( cin, phrase );

        basic_string <char>::size_type phrase_start = 0;
        phrase_start = s.find( phrase );

        if( phrase_start != std::string::npos )
        {
            cout << "Phrase found starting at character position: " << phrase_start << endl;
        }
        else
        {
            cout << "Phrase not found..." << endl;
        }
    }

    return 0;
}

Output:

Code:
Enter the name of the word file: /tmp/gettysburg_address.txt Enter the phrase you want to find in the text contained in file /tmp/gettysburg_address.txt: Four score and Phrase found starting at character position: 0 Enter the name of the word file: /tmp/gettysburg_address.txt Enter the phrase you want to find in the text contained in file /tmp/gettysburg_address.txt: living and dead Phrase found starting at character position: 669 Enter the name of the word file: /tmp/gettysburg_address.txt Enter the phrase you want to find in the text contained in file /tmp/gettysburg_address.txt: Bill and Ted's Excellent Adventure Phrase not found...

As you can see, I loaded the entire file into a dynamically allocated character buffer and then created a string from the buffer. This isn't very efficient, but makes the coding rather simple. This method requires "double" the memory of the file size, so it would be a dog with a relatively large file.


:davis:
  #4  
Old 06-Dec-2007, 13:00
tootypegs tootypegs is offline
Junior Member
 
Join Date: Nov 2007
Posts: 32
tootypegs has a little shameless behaviour in the past

Re: finding words in files


thanks people, i found your posts very helpful
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Named virtual host not working Johnnyrotton Apache Web Server Forum 4 04-Sep-2007 20:32
Undefined reference to pow pisuke C Programming Language 6 02-Aug-2007 08:39
Bloodshed Dev C++ Project Options JdS CPP / C++ Forum 6 11-Nov-2005 17:23
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 21:47
Can't view pages from another machine on the Intranet aevans Apache Web Server Forum 9 14-May-2004 02:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:10.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.