Thread: C newbie with weird problem

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    C newbie with weird problem

    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?

  2. #2
    .
    Join Date
    Nov 2003
    Posts
    307
    Two points: you use strcpy as the name of a variable in
    endwith(). strcpy is defined in the C runtime as an entry
    point for a string copy routine (in string.h which you
    include). Your code probably is whacked out by doing that.

    Second, try this instead of the endswith routine and see if
    it doesn't do what you want:

    Code:
    if(strstr(FileData.cFileName, ".tiff")!=NULL) scanC++;

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    Okay, your code worked to replace the endsWith() function, but it didn't solve my problem. Code there be some buffer problem in the code? I pulled some of it from MSDN and got other tidbits from books and this site.

  4. #4
    .
    Join Date
    Nov 2003
    Posts
    307
    Don't use strcpy as a variable name. You're corrupting memory.

  5. #5
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    I have taken the function endsWith() completely now, thanks for the code peice it worked great. I have thrown a few more debug statements in the code and now I see the problem a bit more clearly:

    When main looks like:

    Code:
    int main(void){
    	searchDir("c:\\users\\clipper\\scan");
    	exit(0);
    
    }
    The program recusivley finds all the tiffs corectly. but when I add to main

    Code:
    int main(void){
    	searchDir("c:\\users\\clipper\\scan");
    	printf("%d files found\n", scanC);
    	exit(0);
    
    }
    the directory is searched twice and I find twice as many .tiff's!!

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > searchDir("c:\\users\\clipper\\scan");
    Where will the extra characters go when you do
    temp = lstrcat(path, "\\");

    Off to never-never land

    You'll do better by doing
    Code:
    char temp[MAX_PATH];
    ...
    sprintf( temp, "%s\\%s", path, FileData.cFileName);
    searchDir(temp);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    I got the counter working. For some reason the call
    Code:
    if(dwAttrs & FILE_ATTRIBUTE_DIRECTORY)
    always is true. I found a way around the problem though. Thanks for all the help.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I found a way around the problem though.
    Care to post it for all to see / learn from?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    5
    here's the code:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <dir.h>
    #include <string.h>
    
    #define BUFSIZE 1024
    
    void searchDir(char *);
    int endsWith(char *, char *);
    
    int scanC = 0;
    
    
    int main(void){
        LPTSTR user;      // pointer to system information string
        DWORD cchBuff = BUFSIZE;    // size of computer or user name
        TCHAR tchBuffer[BUFSIZE];   // buffer for string
    
    	GetUserName(user, &cchBuff);
    
    	searchDir("e:\\users\\clipper\\scan"); //start inital search
    
    	printf("%d files found for %s\n", scanC, user);
        system("pause");
    	return;
    
    }
    
    void searchDir(char *path){
    	WIN32_FIND_DATA FileData;
    	HANDLE hSearch;
    	int check;
    	BOOL fFinished = FALSE;
    	char temp[MAX_PATH];
    
    	//first I move to the directory specified by "path"
    	check = chdir(path);
    	if(check != 0)return;  //if chdir() fails I'm not searching a directory therefore I return
    
    	//get directory listing
    	hSearch = FindFirstFile("*.*", &FileData);
    	if (hSearch == INVALID_HANDLE_VALUE)
    	{
    	    return;
    	}
    
    
    	while (!fFinished)
    	{
    
    		if(strcmp(FileData.cFileName, ".")!=0 && strcmp(FileData.cFileName, "..")!=0){
    				//search on each listing in the directory
    				sprintf( temp, "%s\\%s", path, FileData.cFileName);
    				searchDir(temp);
    		}
    
    		if(strstr(FileData.cFileName, ".tiff")!=NULL)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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about dialogs... weird problem
    By Green Fuze in forum Windows Programming
    Replies: 5
    Last Post: 03-06-2005, 10:04 AM
  2. Weird connection problem.
    By tilex in forum Networking/Device Communication
    Replies: 6
    Last Post: 02-06-2005, 10:11 AM
  3. weird problem with code, not displaying what it should
    By chris285 in forum C++ Programming
    Replies: 2
    Last Post: 01-11-2005, 11:04 AM
  4. weird problem with passing a value..
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 06-21-2002, 09:05 AM
  5. newbie coding problem
    By rippascal in forum C++ Programming
    Replies: 10
    Last Post: 01-08-2002, 11:45 PM