Share via


_chdrive

Changes the current working drive.

int_chdrive(intdrive**);**

Routine Required Header Compatibility
_chdrive <direct.h> Win 95, Win NT

For additional compatibility information, see Compatibility in the Introduction.

Libraries

LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

_chdrive returns a value of 0 if the working drive is successfully changed. A return value of  –1 indicates an error.

Parameter

drive

Number of new working drive

Remarks

The _chdrive function changes the current working drive to the drive specified by drive. The drive parameter uses an integer to specify the new working drive (1=A, 2=B, and so forth). This function changes only the working drive; _chdir changes the working directory.

Example

/* GETDRIVE.C illustrates drive functions including:
 *      _getdrive       _chdrive        _getdcwd
 */

#include <stdio.h>
#include <conio.h>
#include <direct.h>
#include <stdlib.h>
#include <ctype.h>

void main( void )
{
   int ch, drive, curdrive;
   static char path[_MAX_PATH];

   /* Save current drive. */
   curdrive = _getdrive();

   printf( "Available drives are: \n" );

   /* If we can switch to the drive, it exists. */
   for( drive = 1; drive <= 26; drive++ )
      if( !_chdrive( drive ) )
         printf( "%c: ", drive + 'A' - 1 );

   while( 1 )
   {
      printf( "\nType drive letter to check or ESC to quit: " );
      ch = _getch();
      if( ch == 27 )
         break;
      if( isalpha( ch ) )
         _putch( ch );
      if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
         printf( "\nCurrent directory on that drive is %s\n", path );
   }

   /* Restore original drive.*/
   _chdrive( curdrive );
   printf( "\n" );
}

Output

Available drives are:
A: B: C: L: M: O: U: V:
Type drive letter to check or ESC to quit: c
Current directory on that drive is C:\CODE

Type drive letter to check or ESC to quit: m
Current directory on that drive is M:\

Type drive letter to check or ESC to quit:

Directory Control Routines

See Also   _chdir, _fullpath, _getcwd, _getdrive, _mkdir, _rmdir, system