I am writing a c program to recursivly search a path for .tff files and coutn them (Windows 2k). I have a program that does that correctly, but when I try to print out the count I get a weird result. here's the code
Code:
#include <windows.h>
#include <stdio.h>
#include <dir.h>
#include <string.h>
#define DELIM "."
void searchDir(char *);
int endsWith(char *, char *);
int scanC = 0;
int main(void){
searchDir("c:\\users\\clipper\\scan");
printf("%d files found\n", scanC);
exit(0);
}
void searchDir(char *path){
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
BOOL fFinished = FALSE;
char * temp;
chdir(path);
hSearch = FindFirstFile("*.*", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
return;
}
while (!fFinished)
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if(dwAttrs & FILE_ATTRIBUTE_DIRECTORY){
if(strcmp(FileData.cFileName, ".")!=0 && strcmp(FileData.cFileName, "..")!=0){
temp = lstrcat(path, "\\");
temp = lstrcat(temp, FileData.cFileName);
searchDir(temp);
}
}
if(endsWith(FileData.cFileName, "tiff") == 0){scanC++;}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
fFinished = TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}
// Close the search handle.
FindClose(hSearch);
return;
}
int endsWith(char *str1, char *str2){
char *token;
char strcpy[MAX_PATH];
lstrcpy(strcpy, str1);
token = strtok(strcpy, DELIM);
token = strtok(NULL, DELIM);
if(token != NULL)return strcmp(token, str2);
else return 1;
}
the 'printf("%d files found\n", scanC);" in main outputs the last file run through endsWith() (so I think) anyway it spits out a file name (with its path) whats up with that?