Chdir() in C

In this article, we will discuss the chdir() in C with its syntax and example.

What is chdir() in C?

chdir() function or method is used to change the current working directory of a process. It is part of the C standard library, which is found in header files like <unistd.h> on Unix systems and found in <dirent.h> on Windows systems.

Syntax:

It has the following syntax:

Parameters:

Several parameters for the chdir() function are:

path: It is a pointer that specifies the path of the directory to which the current working directory will be changed. It points to a null-terminated string.

Two return values for this function are zero and one. If the directory is changed successfully, then it returns a zero. If any error occurs or if the current directory is not changed, it returns -1, and the errno variable is set to appropriately to indicate the error.

Working of the chdir() function:

The chdir() is a function that lets us switch the working directory of our program to a specified location. Every running program has its idea of the current directory. When we mention file paths without the full directory, the computer starts looking for those files starting from this current directory.

Drawbacks of Chdir() function:

There are several drawbacks of the chdir() function. Some main drawbacks of chdir() function are as follows:

  1. It is not thread-safe. If multiple threads in the program are trying to change the current working directory simultaneously, it throws an error.
  2. It is a platform dependency; it might vary slightly different between operating systems, which may lead to non-portable.
  3. Proper error handling should be done. Sometimes, it requires some permissions to change the directory. It will throw an error if we try to change to a directory that does not exist.

Steps:

  1. First of all, we created one folder named javatpoint.
  2. After that, we created a C language file in the folder javatpoint. Now, we have written the C program for creating two folders named "folder1" and "folder2" in the folder javatpoint.

Example:

Let's take a program to create two subfolders in javatpoint folder in C:

Output:

Chdir() in C

Explanation:

  • <sys/stat.h> contains declarations for the stat structure. It contains mkdir() function.
  • <sys/types.h> contains various datatypes used in system calls. It contains mode_t datatype.
  • mkdir() function is used to create two folders with names "folder1" and "folder2".
  • perror() function is used to display the error message.
Chdir() in C

This article mainly emphasizes the topic of changing from one directory to another directory. I created one file and named it as schange_directiry.c in a folder named javatpoint. Then a program is written to change the directory where the chdir() function is used. When this file is executed in the shell. As an output, we can see the path of a current working directory that represents the directory has changed

Program to demonstrate the chdir() function:

Output:

Chdir() in C

Explanation:

  • In this example, we use the chdir() function to change the directory from folder1 to folder2.
  • getcwd() function used to display the current working directory.
  • perror() is used to print the error message.