Chdir() in CIn 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:
Steps:
Example:Let's take a program to create two subfolders in javatpoint folder in C: Output: Explanation:
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: Explanation:
Next TopicC Programming Test |