chdir

De RAD Studio
Aller à : navigation, rechercher

Remonter à dir.h

Header File

dir.h

Category

Directory Control Routines

Prototype

int chdir(const char *path);

int _wchdir(const wchar_t *path);

Description

Changes current directory.

chdir causes the directory specified by path to become the current working directory; path must specify an existing directory.

A drive can also be specified in the path argument, such as

chdir("a:\\BC");

but this method changes only the current directory on that drive; it does not change the active drive.

  • Under Windows, only the current process is affected.

Return Value

Upon successful completion, chdir returns a value of 0. Otherwise, it returns a value of -1, and the global variable errno is set to

ENOENT

Path or file name not found


#include <stdio.h>
#include <stdlib.h>
#include <dir.h>
char old_dir[MAXDIR];
char new_dir[MAXDIR];
int main(void)
{
  if (getcurdir(0, old_dir))
  {
      perror("getcurdir()");
      exit(1);
  }
  printf("Current directory is: \\%s\n", old_dir);
  if (chdir("\\"))
  {
      perror("chdir()");
      exit(1);
  }
  if (getcurdir(0, new_dir))
  {
      perror("getcurdir()");
      exit(1);
  }
  printf("Current directory is now: \\%s\n", new_dir);
  printf("\nChanging back to original directory: \\%s\n", old_dir);
  if (chdir(old_dir))
  {
      perror("chdir()");
      exit(1);
  }
  return 0;
}


POSIX Win32 ANSI C ANSI C++

chdir

+

+

_wchdir

NT only