_chdrive

Description

Changes the current working drive.

#include <direct.h> Required only for function declarations  

int _chdrive( int drive );

drive Number of new working drive  

Remarks

The _chdrive function changes the current working drive to the drive specified by drive. The drive argument uses an integer to specify the new working drive (1=A, 2=B, etc.).

This function changes only the working drive; the _chdir function changes the working directory.

With DOS, the new drive set by the program becomes the new working drive.

Return Value

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

Compatibility

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

_chdir, _dos_setdrive, _fullpath, _getcwd, _getdrive, _mkdir, _rmdir, system

Example

/* GETDRIVE.C illustrates drive functions including:

* _getdrive _chdrive _getdcwd

*/

#include <stdio.h>

#include <conio.h>

#include <direct.h>

#include <stdlib.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. This is only necessary for DOS. Under OS/2

* the current drive of the calling process is always restored.

*/

_chdrive( curdrive );

printf( "\n" );

}

Output

Available drives are:

A: B: C:

Type drive letter to check or ESC to quit: q

Type drive letter to check or ESC to quit: a

Current directory on that drive is A:\

Type drive letter to check or ESC to quit: c

Current directory on that drive is C:\LIBREF

Type drive letter to check or ESC to quit: