_chdrive odd results

This is a discussion on _chdrive odd results within the Windows Programming forums, part of the Platform Specific Boards category; Okay maybe I'm doing this completely wrong, and it does seem like a very basic question. But even using MSDN ...

  1. #1
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66

    _chdrive odd results

    Okay maybe I'm doing this completely wrong, and it does seem like a very basic question. But even using MSDN examples I believe I have it exactly the same. With some slight changes for better error checking. Well here is the code:

    Code:
    // find all available hard drives
    		for( i=0 ; i<26 ; i++ )
    		{
    			printf("Checking %c drive.\n", i+'A');
    			// available drive
    			if( !_chdrive(i) )
    			{
    				// start scanning drive
    				printf("Scanning drive %c.\n", i+'A');
    				sprintf(path, "%c:\\*", i+'A');
    				hFind = FindFirstFile(path, &FindFileData);
    				if( hFind == INVALID_HANDLE_VALUE )
    				{
    					switch(GetLastError())
    					{
    						case 3:							return(NULL);						break;
    
    						default:
    							printf("Cannot scan through drive %c. (%d)\n", i+'A', GetLastError());
    							break;
    					}
    				}
    				else ScanFolder(pfFiles, path, search, hFind, FindFileData);
    			}
    		}
    Now my problem is this: My program will only try to scan through the D drive (and I don't have a D drive). But will skip over A, C, E, F, etc. all the drive letters that I actually am using. Can anyone tell me why this is? ScanFolder is just a function that is recursively setup to scan through an entire directory for the search criteria with each file. Each time a new folder is found, it will call itself to scan through the new directory. But it doesn't even get that far so I haven't been able to test it.

    Thanks,
    cyreon

  2. #2
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Contrary to most programming practices _chdrive(drive_number) is not 0 based but 1 based, so 1 = a, 2=b, and 3 = c, (and not D and your code will report) 0 is an invalid drive.

  3. #3
    uh oh
    Join Date
    Jan 2005
    Location
    Ontario, CA
    Posts
    66
    Thanks. That fixed the problem, with a couple other adjustments.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd results.. help?
    By MCyborg in forum C Programming
    Replies: 15
    Last Post: 11-14-2005, 07:24 PM
  2. Odd results for such a simple procedure.
    By tyouk in forum Windows Programming
    Replies: 2
    Last Post: 01-14-2005, 08:16 PM
  3. C Subroutine produces odd results
    By IGAU in forum C Programming
    Replies: 8
    Last Post: 11-24-2003, 09:47 AM
  4. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 03:39 PM
  5. Homework help
    By Jigsaw in forum C++ Programming
    Replies: 2
    Last Post: 03-06-2002, 05:56 PM

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