How to write a function that search for a specific string in a text file?

I am trying to write a program that can search or find a character string in a text file.
Which means when you ifstream and infile a text and then search for a string.
For example in the following sentence search for the word of "direct":

Peter is a self-direct learner.

How can I start writing a function for this?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danieldaniel_2000Author Commented:
I really don't know how to write this, but i think that I should start this program as a function first.
Since you are trying to learn, I won't give you the answer (yet).  I'll give you a couple of peices to get started.

You can read an a line from a file into a STL string using

i#include <string>
#include <fstream>

using namespace std;

    *  *  *  // meaning suff skipped

  // then inside a function
  fsteam Fil("somefilename",ios_base:in);
  string Line;

  getline(Fil,Line);  // Read a line into "Line"

you can search a string for another string usign the "find_first_of()" procedure, like

int pos = Line.find_first_of("text to look for");

Try to work with that, let me know if you have any questions.
danieldaniel_2000Author Commented:
Sorry, but I am not familiar with the namespace std, therefore I don't want to use that as a part of my program
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

if you want to find the word in the string you have to compare characters. So you work your way through the text file untill you reach the first letter of the word your looking for.  Once you find the first letter somewhere you check the next character after that, if it's the same as what the next letter in the word, then move to the next letter.  you keep doing this untill either you reach the end of the word( in this case you would have found a case of the word), or if one of the letters don't match and your not at the end of the word then then start searching the text for the first letter in the word again.  

The main part of the function would be a loop that goes to the next character and then compares the letters to the letters in the string.  The function would probably take the string to search for and a file name(directory in cluded)as parameters.  and The output depentds on you, ex. you can have it print out a statment telling what line and column the letter is on.

So you would have

void findString(char word[], char file[])
create a input file stream for the text
open the file

   get a character
   compare it with first letter in word
   if character == first letter in word
       then compare the rest of the letter s in the word
// you can to the rest of the comparison in another function.

   if word is found
       then print out line and coulumn or whatever you would like to do
   move to next character in the text

}stop loop when end of text is reached

you will probably need to #include <iostream.h> or some other similar file.  

Good luck I hope this helps.
You are not familar with the answer, right?  Isn't the point that you want to learn?
danieldaniel_2000Author Commented:
Can any body give me a more complete and detailed explanation?
I am recently working on C++ programming, therefore I need more simple code for this program.
From the other question you have an example that shows you how to read the whole file into a string.  Once it is in the string just use find_first_of().

int Pos = Txt.find_first_of("direct");
cout << "Match found in file at " << Pos;

to find the first place where "direct" apears in the text.

If you load the text into a vector of arrays, you have to search each line seperately, like

int LineCount = Txt.size(); // Number of lines stored.

for (int i=0; i < LineCount; ++i)
   int Pos = Txt[i].find_first_of("direct");
   if (Pos != string::npos)
       cout << "Match found on line " << i << "at column " << Pos;
danieldaniel_2000Author Commented:
Here is how I infile without using string, but how can I search for a word in this text and replace it with another word. Using a function.
Can anybody give me a help.
int original()
      int i=1;

      ifstream infile;   "file.txt",ios::in);      
      if (infile)
            for (i=1;i<=27;i++)
cout << readout[i].moors << endl;
cout << "An error occurred while opening the file." << endl;
      return 0;
It would be good for us to know what "readout" is.

The code reads up to 99 characters.  Then it ignores up to 80 characters until it reaches the end of a line.  Is that really what you want?  You may be skipping data that way.  It could be what you want, but I doubt it.

assumung readoud[i].moors is a character array, then you can use strstr() to find the word inside the moors string.  like

char *WordPtr = strstr(readout[i].moors,"Hello");

if (WordPtr)
   cout << "\"Hello\" has been found.";
danieldaniel_2000Author Commented:
Is strstr will give you the position of the word "Hello", but if I want to replace it what I need to do?
Using a for loop to change or what?
Do you want to replace it with text of the same length?  If so you can copy in the text using memcpy().  what this does is copy the new characters into the exact same place where the old characters were stored and leaves the characters before and after unaltered, like for example

char Line[] = "123abc7890";

char *ABCPtr = strstr(Lin,"abc");
// ABCPtr now points to "abc"

// replaces the "abc" with "456".


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Now if the repalcement text is a different length than the text it is replacing, then you will have to do more work.  One solution is to "cosntruct" a new string by copying the characters before the replacement text, copying the replacement text, then copying the characters after the replacement text.

char Line[11] = "123ab7890";

char *ABPtr = strstr(Lin,"ab");
 // ABPtr now points to "abc"
// we want to put in "456" but it won't fit in two
// spaces, so the '6' will overwrite the '7' if we do that.

char Temp[11];

int Length1 = ABPtr - Line;
// This length is the length of the characters before the "ab"
// It should be 3  ("123" is 3 characters long).  When you
// subtract pointers you get the number of items "between"
// them.  "ABPtr" points to Line[3] and "Line" is a pointer
// to Line[0] so their difference is 3.  (and "array name" is
// just a pointer to the first item in the array.  so "Line" is
// a pointer in every way, except you are not allowed to
// make it point to anythen else.).

// copy the 1st part.

// copy in new text

// copy in the final text.
// Note that adding 2 to ABPtr meakes it point past
// the 2 two characters it found ("AB") so it now points
// to the "7" of "7890".

// copy the new string back.

Note this is much much much easier if you use STL strings!
danieldaniel_2000Author Commented:
It does work for the same length, but not for the another one it compile but when it links the compiler says that it has performed illegal operation and the program will shut down.
I don't know why.
danieldaniel_2000Author Commented:
I try the function you gave me , and it doesn't work in a big text file it works only in a single string, i mean the first string. Also it output only the string I wanted to change, and then the entire file is gone when I cout.
What is the code you used?

Have you tried debugging to see where it is crashing?
danieldaniel_2000Author Commented:
It crash in the part where is strstr(code, "").
I think maybe you can not change the memory because it is an array of string.
It says access violation in the compiler
There are many ways to uyse strstr() that might cause it to crash.  There are many ways that it won't.  That is why I asked to see the code.  I can't guess at what you are doing.
danieldaniel_2000Author Commented:
Thanks a lot
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.