• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1148
  • Last Modified:

C++ searching for a line in a text file

I want to analyze the content of a text file & search for a string in the file.
It is assumed a sentence is terminated by a ., ? or !

Lets say the text file named summary.txt contains the following

Create or save more than 3.5 million jobs over the next two years;
Take a big step toward computerizing Americans health records, reducing medical
errors, and saving billions in health care costs;
Revive the renewable energy industry and provide the capital over the next three years.

If i were to search for the word 'The' , it would return

Create or save more than 3.5 million jobs over the next two years;

Revive the renewable energy industry and provide the capital over the next three years.

Can someone help me out
#include <iostream>
#include <fstream>
 
using namespace std;
 
int main()
{
    string x;
    string name;
    ifstream fin;
    
    cout << "Enter a word to search for: ";
    cin >> x;
    cout << "You entered the word: " << x << endl;
 
    fin.open("test.txt");
    
    if (!fin.good()) 
    {
        cout << "File not found" << endl;
		return 1;
    }
	
	while(!fin.eof())
 
	{
        getline(fin,x);
        cout << "Word is : " << x << endl;
        
    }
        
    fin.close();
    system("pause");
    return 0;
}

Open in new window

summary.txt
0
passaged
Asked:
passaged
  • 4
  • 3
1 Solution
 
Infinity08Commented:
Since a line in the text file doesn't necessarily correspond to a sentence, you can't process the file line by line. You need to read enough lines from the file until you have at least one complete sentence, before you can decide whether the sentence contains the word you're looking for.

Even easier (for relatively small files), is to read the whole file into a memory buffer (a string), and then process that string, and split it up into sentences. Then, for each of those sentences, you can check whether it contains the word.
0
 
passagedAuthor Commented:
Do you have any example for that ?
0
 
Infinity08Commented:
For what part ?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
passagedAuthor Commented:
I mean do you have a code sample , i can look through for the second part of your answer

"Even easier (for relatively small files), is to read the whole file into a memory buffer (a string), and then process that string, and split it up into sentences. Then, for each of those sentences, you can check whether it contains the word."
0
 
Infinity08Commented:
>> I mean do you have a code sample

Yes, I know, but what part of my suggestion do you want an example of ? I described the whole program - giving an example of the whole program is a bit silly, isn't it ? ;) So, which part are you unsure about how to do it ?
0
 
passagedAuthor Commented:
processing the string and splitting it up into sentences
0
 
Infinity08Commented:
Here's one possible approach :

You can use find_first_of :

        http://cplusplus.com/reference/string/string/find_first_of/

to find the end of the next sentence (by looking for a '.', '!' or '?').

You can then use substr :

        http://cplusplus.com/reference/string/string/substr/

to copy the whole sentence to a new string.

And finally, you can look for the word inside that new string using find :

        http://cplusplus.com/reference/string/string/find/


If you want code examples, check the reference pages - they each contain clear code examples of how to use the string methods.
0
 
ltgbauCommented:
Here is the sample code, if you compile error, check header file plz.
If it encount error whem compiling I think it can give you some algorithm suggestion.
good luck :D
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <string>
#include <vector>
 
void findWord(const char *pData, int nLength, const char* szWord, std::vector<std::string> *arrResult);
 
int _tmain(int argc, _TCHAR* argv[])
{
        FILE *file = NULL;
        file = fopen("c:\\ee.txt", "rb");
        if(file!=NULL)
        {
                int nLength = _filelength(file->_file);
                if(nLength>0)
                {
                        char *pData = (char*)calloc(nLength+1, sizeof(char));
                        if(pData!=NULL)
                        {
                                size_t nRead = fread(pData, sizeof(char), nLength, file);
                                if(nRead==nLength)
                                {
                                        // process
			    std::vector<std::string> arrResult;
                                        findWord(pData, nLength, the, &arrResult);
                                        free(pData);
                                        pData = NULL;
                                }
                        }
                }
                fclose(file);
        }
        printf("\nPress any key to exit...");
        getchar();
        return 0;
}
 
void findWord(const char *pData, int nLength, const char* szWord, std::vector<std::string> *arrResult)
{
        char *pTempData = (char*)calloc(nLength+1, sizeof(char));
        memmove(pTempData, pData, nLength*sizeof(char));
        char seps[] = ".?!";
        char *token;
        token = strtok(pTempData, seps );
        while( token != NULL )
        {
                char* lpszFound = strstr(strtok, szWord);
	    if(lpszFound!=NULL)
	    {
		arrResult->push_back(std::string(lpszFound));
     }
                token = strtok( NULL, seps );
        }
        free(pTempData);
}

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now