Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How2 Seach file for string ???

Status
Not open for further replies.

skinah

Programmer
Oct 3, 2001
17
AU
I'm slowly learning visual c++ and need a little bit of help.

How do I search the contents of a file for a string ?

For example I wish to search a text file say (test.txt) that I know the name of for the words "hello" . I dont know the exact location of the words and dont care about the location, I just need to know if the file contains "hello" and then execute some commands if it exsists.

Any help would be apreeciated and source code to pull apart also helps me heaps.
 
You can use STL stuff... X-)
Code:
#include <string>
#include <fstream>
#include <iostream>

int main()
{
	std::ifstream file( &quot;c:\\test.txt&quot; );
	if ( ! file )
		return 1;

	bool found = false;

	do
	{
		std::string line;
		std::getline( file, line );
		if ( line.find( &quot;hello&quot; ) != std::string::npos )
			found = true;			

	} while( file && ! found );

	if ( found )
		std::cout << &quot;we got it!\a&quot; << std::endl;

	return 0;
}
 
Do it the hard way:

Include the header(
Code:
#include <stdio.h>
)
Declarate the buffer(
Code:
char a;
)
Declarate the exit(
Code:
bool exit;
)
Declarate the position(
Code:
unsigned long int pos;
)
Open a file (
Code:
FILE *b = fopen(&quot;C:\\file&quot;, &quot;r&quot;);
)
Make it loop while it doesn't find the text(
Code:
while((exit == FALSE) && (a != EOF))
{
    a = fgetc(b);
    pos++;
    if(a == 't')
    {
        a = fgetc(b);
        if(a == 'e')
        {
            a = fgetc(b);
            if(a == 'x')
            {
                a = fgetc(b);
                if(a == 't')
                {
                    exit = TRUE;
                    pos--;
                }
            }
        }
    }
}
)
Then you will know the &quot;text&quot; starts at pos. You may even continue what you will do in the ifs. All together it'll be like this:
Code:
#include <stdio.h>

char a;
bool exit;
unsigned long int pos;

FILE *b = fopen(&quot;C:\\file&quot;, &quot;r&quot;);

while((exit == FALSE) && (a != EOF))
{
    a = fgetc(b);
    pos++;
    if(a == 't')
    {
        a = fgetc(b);
        if(a == 'e')
        {
            a = fgetc(b);
            if(a == 'x')
            {
                a = fgetc(b);
                if(a == 't')
                {
                    exit = TRUE;
                    pos--;
                }
            }
        }
    }
}
;-) Thank you,

JVFF (Janito Vaqueiro Ferreira Filho)
 
I assume this is a joke - when you want to find something else you write a new program... :) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top