C – Directory Functions

Home/Learn C Programming/C – Directory Functions
C – Directory Functions 2017-08-07T14:52:55+00:00

Directory Functions :

getcwd () :

getcwd() function is used to get the current working directory.

Syntax :

char * getcwd(char * buf, int buflen);

C – GETCWD() DIRECTORY EXAMPLE PROGRAM :

chdir :

chdir command is used to change the current working directory

Syntax :

int chdir(const char *path);

Return value returns 0 if success, else non zero.

Output :

Enter path :

E:\C\programs

We will get the message “Directory Changed”

To check whether we have changed to that specific directory or not, run the getcwd.c program in that directory, then it will display the current working directory as : E:\C\programs.

mkdir :

mkdir command is used to create a directory.

Syntax :

int mkdir(const char *path);

Returns 0 if success, else non zero.

C – MKDIR COMMAND EXAMPLE PROGRAM :

Output :

Enter path :

cprogram

Directory is created in our current directory.

If we gives the same path once again, we will get “Unable to create a dir”

Go to command prompt and type dir, then directory cprogram is displayed at the last

rmdir :

rmdir command is used to remove a dos file directory.

Syntax :

int rmdir(const char *path);

Returns 0 if success, else non zero.

rmdir  deletes the directory whose path is given by the path. The directory named by the path ,

  • must be empty
  • must not be current working directory
  • must not be the root directory.

C – RMDIR COMMAND EXAMPLE PROGRAM :

Output :

Goto DOS shell

E:\c\programs> md language

E:\c\programs> cd language

Directory created

E:\c\programs\language> copy con aa.txt

Good morning

Welcome

Ctrl + z

(1) file is copied

 

E:\c\programs> type aa

Then the above data is displayed

E:\c\programs> cd ram

Directory created

Now our current working directory has 2 sub directories, called language and ram.

We can delete the sub directory ram by using rmdir(), but we cannot deleted the directory language, because language contain one text file aa.txt, but ram is empty, hence we can remove it.

If we want to deleted the directory language, then again goto DOS shell

E:\c\programs\language> del aa.txt

  • file deleted.

Now we can delete the folder language with the help of rmdir().

Prev Next