Scanning strings.

Posts 1–5 of 5 · Page 1 of 1
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:
Code:
dddddd david
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.
Not a very in-depth solution, but I would use strcmp instead. strpbrk really isn't helping you at all. Just iterate through the file string and when str1[0] = str2[0] start keep going through so that once you finish looping through all chars and they're all the same you return the file handle or whatever.
Anyone thought of strstr?
StrStr Function (Windows)

Just read in line by line, use strstr and check if the return is not NULL(which means string was found) and either use break to get out of the scanloop or let it continue to find multiple occurances.

With that you can go as detailed as line number and character offset from the start of the string
Well that sure makes life easier. =/
Didn't know that existed.
Posts 1–5 of 5 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us