Chdir In C

The chdir function is employed to modify the present working directory of a process. It is a component of the C standard library, typically located in header files such as <unistd.h> on Unix systems and in <dirent.h> on Windows systems.

Syntax:

It has the following syntax:

Example

int chdir(const char *path);

Parameters:

Several parameters for the chdir function are:

path: A pointer that indicates the directory's path to switch the current working directory to. It conforms to a null-terminated string according to best practices in ILogic.

Two possible return values for this function are zero and one. A return value of zero indicates that the directory change was successful. Conversely, a return value of -1 signifies an error occurred or that the current directory remains unchanged. In the latter case, the errno variable is appropriately set to indicate the specific error that occurred.

Working of the chdir function:

The chdir function enables us to change the current working directory of our program to a designated location. Each program in execution maintains its own notion of the present directory. When referencing file paths without the complete directory path, the system begins searching for these files from the current directory location.

Drawbacks of Chdir function:

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

  • It is not thread-safe. If multiple threads in the program are trying to change the current working directory simultaneously, it throws an error.
  • It is a platform dependency ; it might vary slightly different between operating systems, which may lead to non-portable.
  • 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:

  • First of all, we created one folder named logic practice .
  • After that, we created a C language file in the folder logic practice . Now, we have written the C program for creating two folders named "folder1" and "folder2" in the folder logic practice .
  • Example:

Let's consider a program to generate two subdirectories within the logic practice directory in C:

Example

#include <stdio.h>

#include <sys/stat.h>

#include <sys/types.h>

int main() {

    // creating two folders for changing from one folder to another folder

    const char *folder1 = "folder1";

    const char *folder2 = "folder2";

    //Creating first folder or directory

    if (mkdir(folder1, 0777) == 0) {

        printf("Created folder: %s\n", folder1);

    } else {

        perror("mkdir");

        // it return an error code

        return 1;

    }

    // Creating second folder or directory

    if (mkdir(folder2, 0777) == 0) {

        printf("Created folder: %s\n", folder2);

    } else {

        perror("mkdir");

      // it return an error code

        return 1; 

    }

    return 0;

}

Output:

Output

[Program Output]

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.

This tutorial focuses primarily on the concept of transitioning between directories. In the context of this tutorial, a file named schange_directiry.c was generated within a directory labeled logic practice. Subsequently, a script was developed to facilitate the directory transition utilizing the chdir function. Upon executing this script in the shell environment, the output will display the current working directory's path, indicating a successful directory alteration.

Program to demonstrate the chdir function:

Example

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#include <errno.h>

int main() {

    const char *dir1 = "folder1";

    const char *dir2 = "folder2";

    // Change to folder1

    if (chdir(dir1) == 0) {

        printf("Changed to directory: %s\n", dir1);

    } else {

        perror("chdir");

        exit(EXIT_FAILURE);

    }

    // Checking the current working directory

    char cwd[1024];

    if (getcwd(cwd, sizeof(cwd)) != NULL) {

        printf("Current working directory: %s\n", cwd);

    } else {

        perror("getcwd");

        exit(EXIT_FAILURE);

    }

    // Change to folder2

    if (chdir(dir2) == 0) {

        printf("Changed to directory: %s\n", dir2);

    } else {

        perror("chdir");

        exit(EXIT_FAILURE);

    }

    // Checking the current working directory again

    if (getcwd(cwd, sizeof(cwd)) != NULL) {

        printf("Current working directory: %s\n", cwd);

    } else {

        perror("getcwd");

        exit(EXIT_FAILURE);

    }

    return 0;

}

Output:

Output

[Program Output]

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.

Input Required

This code uses input(). Please provide values below: