Scanning strings.
Hello.
This might be a pretty choob-ish thread, but I can't figure this out. What I'm trying to do it scan a directory of all it's files, then scan each file for a string. I've got the directory/file part down. The part I'm having troubles with is breaking at each character and comparing the strings.
This is what I've got so far:
[PHP]
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
char* DirectoryPath = "C:\\Documents and Settings\\David\\My Documents\\TestFolder";
char* Search = "david";
int main()
{
FILE* File;
string FileList[MAX_PATH];
int num = 0;
WIN32_FIND_DATA FileData;
char Path[MAX_PATH];
sprintf(Path,"%s\\*",DirectoryPath);
HANDLE FoundFile = FindFirstFile(Path,&FileData);
if(FoundFile != INVALID_HANDLE_VALUE)
{
do{
if(!(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
FileList[num] = FileData.cFileName;
num++;
}
}while(FindNextFile(FoundFile,&FileData));
}
for(int i=0;i<num;i++){
string CurrentLine;
static char FilePath[MAX_PATH];
sprintf(FilePath,"%s\\%s",DirectoryPath,FileList[i].c_str());
File = fopen(FilePath,"rb");
if(!File){ cout <<"File could not be open."; cin.get(); return 0; }
fseek(File,0,SEEK_END);
long Size = ftell(File);
rewind(File);
char* Buffer;
Buffer = (char*)malloc(Size);
fread(Buffer,1,Size,File);
char* Ptr = strpbrk(Buffer,Search);
do
{
char temp[MAX_PATH] = {0};
for(int i=0;i<strlen(Search);i++)
{
temp[i] = *(Ptr+i);
}
if(strcmp(temp,Search)==0)
{
cout << "String found in: "<< FilePath <<"\n\n";
} else {
Ptr = strpbrk(Ptr+1,Search[0]);
}
}while(Ptr != NULL);
fclose(File);
}
cin.get();
}
[/PHP]
Right now, it will only output "String found in: this file" if the first occurance of the character is the correct one. For example. I have this in a text file:
And I search "david" using my method, it will break at the first 'd', check if the preceding 4 letters are 'avid' and if it isn't. It just stops. I don't know how to keep reading from where it stops. As you can see I've tried breaking starting from where the first character starts +1, so that it skips the character it broke at earlier. It doesn't seem to work.
Yeah.. if any of you has an easier way to scan strings or buffers, let me know. This method seems a little over complicated.
Thanks.