[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]


chdir

Change the current directory.

Compatibility:

This function is compatible with the following targets:

ANSI

BeOS

EMB/RTOS

Mac OS

Palm OS

Win32


Prototype:
#include <unistd.h>
int chdir(const char *path);
Parameters:

Parameters for this facility are:

path  
char *  
The new pathname  

Remarks:

The function chdir() is used to change from one directory to a different directory or folder. Example of usage is given in "Example of chdir() usage."

Return:

chdir() returns zero, if successful. If unsuccessful chdir() returns negative one and sets errno.

See Also:

"Overview of errno.h"

Example of chdir() usage.:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stat.h>

#define SIZE FILENAME_MAX
#define READ_OR_WRITE	0x0 /* fake a UNIX mode */

int main(void)
{
char folder[SIZE];
char curFolder[SIZE];
char newFolder[SIZE];
int folderExisted = 0;

	/* Get the name of the current folder or directory */
	getcwd( folder, SIZE );
	printf("The current Folder is: %s", folder);

	/*  create a new sub folder */
	/* note mode parameter ignored on Mac */
	sprintf(newFolder,"%s%s", folder, "Sub" );
	if( mkdir(newFolder, READ_OR_WRITE ) == -1 )
	{
		printf("\nFailed to Create folder");
		folderExisted = 1;
	}
	
	/* change to new folder */
	if( chdir( newFolder) ) 
	{
		puts("\nCannot change to new folder");
		exit(EXIT_FAILURE); 
	}
	
	/* show the new folder or folder  */
	getcwd( curFolder, SIZE );
	printf("\nThe current folder is: %s", curFolder);
	
	/* go back to previous folder */
	if( chdir(folder) ) 
	{
		puts("\nCannot change to old folder"); 
		exit(EXIT_FAILURE); 	
	}

	/* show the new folder or folder  */
	getcwd( curFolder, SIZE );
	printf("\nThe current folder is again: %s", curFolder);
	
	if (!folderExisted)
 	{ 
 	/* remove newly created directory  */
	if (rmdir(newFolder))
  		{
   		puts("\nCannot remove new folder");
   		exit(EXIT_FAILURE); 
  		}
  		else
   		puts("\nNew folder removed");

  	/* attempt to move to non-existant directory  */
  		if (chdir(newFolder))
   	puts("Cannot move to non-existant folder");
 	}
 	else  		puts("\nPre-existing folder not removed");

	return 0;
}

Output 
The current Folder is: Macintosh HD:C Reference:
The current folder is: Macintosh HD:C Reference:Sub:
The current folder is again: Macintosh HD:C Reference:
New folder removed
Cannot move to non-existant folder


[ First ]  [ Previous ]  [ Next ]  [ Last ]  [ Manuals ]

Visit the Metrowerks website at: http://www.metrowerks.com
For assistance contact Metrowerks Technical Support at: cw_support@metrowerks.com
Copyright © 2000, Metrowerks Corp. All rights reserved.

Last updated: August 16, 2000