directory searching causes a windows error

This is a discussion on directory searching causes a windows error within the Windows Programming forums, part of the Platform Specific Boards category; BobS0327 The code you posted seems work, but as soon as all the files are searched it closes the program ...

  1. #16
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    BobS0327

    The code you posted seems work, but as soon as all the files are searched it closes the program with exit status of 1. I'm trying to figure out why, or where it could be coming from.

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Quote Originally Posted by BobS0327 View Post
    Unfortunately, I couldn't understand your logic. So, I modified your function as follows:
    The total variable was used to count the total number of files found.
    And you're back to the same old code. You've split duplicate code into two branches. Bad.

    I merged the two branches into one. Something akin to this. I haven't tested it, however.
    Code:
    bool Search(const char* sFolder, int& fCount, int& dCount, int& total)
    {
    	char* fBuffer = new char[256]; //storage for file name to be checked
    
    	WIN32_FIND_DATA Files;  //structure for handling all the files
    	HANDLE hFiles = NULL;  //handles for FindNextFile()
    	strcpy(fBuffer, sFolder); //set up new folder to search in
    	strcat(fBuffer, "\\*.*"); //find all files in new folder
    
    	hFiles = FindFirstFile(fBuffer, &Files); //find the first, associated with files
    	if (hFiles == INVALID_HANDLE_VALUE)
    	{
    		do 
    		{
    			// Watch out for buffer overflows!
    			assert( (strlen(sFolder) + 1 + strlen(Files.cFileName) + 1) <= 256 );
    			sprintf(fBuffer, "&#37;s\\%s", sFolder, Files.cFileName);
    			if (Files.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && 
    				(strcmp(Files.cFileName, ".") != 0) &&
    				(strcmp(Files.cFileName, "..") != 0)) {
    					++dCount;
    					Search(fBuffer, fCount,dCount, total);
    			} 
    			else 
    			{
    				if((strcmp(Files.cFileName, ".") != 0) && (strcmp(Files.cFileName, "..") != 0))
    				{
    					++fCount;
    					printf("cFileName : %s\n", Files.cFileName);
    				}	
    			}   
    		}
    		while(FindNextFile(hFiles,&Files) != 0);
    		//{
    		//	sprintf(fBuffer, "%s\\%s", sFolder, Files.cFileName);
    		//	if (Files.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && 
    		//		(strcmp(Files.cFileName, ".") != 0) &&
    		//		(strcmp(Files.cFileName, "..") != 0))
    		//	{
    		//		++dCount;
    		//		Search(fBuffer, fCount,dCount, total);
    		//	}        
    		//	else
    		//	{
    		//		if((strcmp(Files.cFileName, ".") != 0) && (strcmp(Files.cFileName, "..") != 0))
    		//		{
    		//			++fCount;
    		//			printf("cFileName : %s\n", Files.cFileName);
    		//		}
    		//	}
    		//}
    		//FindClose(hFiles );
    	}
    	delete [] fBuffer;
    	return (hFiles != INVALID_HANDLE_VALUE);
    }
    And beware where I placed an assert to check for buffer overflows!
    Last edited by Elysia; 04-02-2008 at 05:40 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,059
    I merged the two branches into one. Something akin to this. I haven't tested it, however.
    Unfortunately, your code doesn't even compile correctly, let alone process the folders recursively. I would strongly suggest that you read some tutorials on recursively processing files/folders. It's unfortunate that most of the time you don't know what you're talking about since you don't do your homework before posting a response to a thread.

    Finally, TEST your code postings before you post it to at least make sure it works.

  4. #19
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    Quote Originally Posted by BobS0327 View Post
    Unfortunately, your code doesn't even compile correctly, let alone process the folders recursively. I would strongly suggest that you read some tutorials on recursively processing files/folders. It's unfortunate that most of the time you don't know what you're talking about since you don't do your homework before posting a response to a thread.
    This is untrue. The original example code was my own and it works 100% fine.
    I DO know what I talk about most of the times.

    Finally, TEST your code postings before you post it to at least make sure it works.
    Most people don't do that. Go blame everyone else.
    If you don't want the code, then fine. It shows about how it should look. It's not a complete "make your homework for you".

    I'll update the code to fix some compile errors.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Elysia
    Your code works great, and does not have the problem of closing once it's done searching. It did however do that before I changed the assert compare size, and buffer size. I think it would close because the buffer got overloaded, could not hold the new path, then the new path was incomplete, and forced an error and closed. Anyways, I changed the values to 999 just to make sure there is plenty of room for a long path. I also noticed some things in there that would make it not work correctly, but i think that was done on purpose

    I kind of feel bad cause I did not figure the end code out on my own, but yet I was obviously having a hard time. Thanks for all your help, especially Elysia!

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    My goodness. I just realized the code actually doesn't call CloseHandle >_<
    That goodness for RAII, though. I'll fix that right now!

    New code looks something like:
    Code:
    		hFileDirs = FindFirstFile(pArgs->strFolder + _T("\\*.*"), &fDataDirs);
    		CHandle hhFileDirs(hFileDirs);
    		if (hFileDirs == (HANDLE)-1) return false; // Apparently we couldn't search this directory...
    		hFileFiles = FindFirstFile(pArgs->strFolder + _T("\\") + pArgs->strPattern, &fDataFiles);
    		CHandle hhFileFiles(hFileFiles);
    Last edited by Elysia; 04-02-2008 at 09:05 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Elysia View Post
    My goodness. I just realized the code actually doesn't call CloseHandle >_<
    Are you talking about your original code you posted for me to look at?

    Another question. How would I be able to tell once it is done searching? It's recursive so putting a call at the end of the function is out. I tried putting checks to see if it added a count to a file -and- directory, but once it hits the end of a folder it automatically does not increase either's count, so thats out. I thought maybe making the hFiles handle global and checking when it returns invalid but it never returns invalid as long as it is an existing directory. So.... what are the options here?
    Last edited by scwizzo; 04-02-2008 at 03:09 PM.

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    FindNextFile returns FALSE and GetLastError() returns ERROR_NO_MORE_FILES when there's no more files in the directory.

    And yes, the original code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    Quote Originally Posted by Elysia View Post
    FindNextFile returns FALSE and GetLastError() returns ERROR_NO_MORE_FILES when there's no more files in the directory.
    Exactly, so everytime it reaches the end of a directory it will think it's done, when really it's not. It's only done if the function was not called anymore and all other functions are done, but that happens at the end of every directory. So I don't know how to make it know the searching is done.

  10. #25
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,771
    But the point is, when it's done, it return true or false, until all the functions have finished.
    It will recursively call itself for every directory, so when all those calls are done, the first instance of the function continues and it has found all the directories and files in its own directory and returns.
    I don't see what the problem is?

    Main won't continue until your function is finished.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #26
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    I see what you're saying. Sometimes I think my brain just doesn't like to work.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21