Here are three ways in which we can get the path to the current working directory using a c program,
1. we can make use of the function "getcwd".
Header file required : unistd.h
The syntax of getcwd is
buf: Is the character pointer in which the path of current working directory is stored. size: Is the maximum size of the path in bytes, if the size exceeds this then a NULL is returned
Here is a program that shows the usage of getcwd.
Save the program is get_cwd.c. Compile and execute it.
2.using get_current_dir_name.
Header file required : unistd.h
Syntax:
The function returns the absolute path to the current working directory. The function itself mallocs for an array big enough to hold the current directory name, thus we don't need to do malloc in the program. This function returns the value of the environment variable PWD, thus if it is not set the function might fail to return the current value.
Here is a program which uses get_current_dir_name to get the current working directory.
Save the file as get_current_dir.c. Compile and execute it.
3. The third way of getting the current working directory is using the command "pwd" along with the function "system". The system function executes which ever command is passed to it and prints the result on the screen. Thus we can execute the command system and pass the command "pwd" as the argument, the result of which will be the current working directory being printed on the screen.
The following program shows how the same can be implemented.
Save the file as get_system_pwd.c. Compile and execute the same.
1. we can make use of the function "getcwd".
Header file required : unistd.h
The syntax of getcwd is
buf: Is the character pointer in which the path of current working directory is stored. size: Is the maximum size of the path in bytes, if the size exceeds this then a NULL is returned
Here is a program that shows the usage of getcwd.
Save the program is get_cwd.c. Compile and execute it.
2.using get_current_dir_name.
Header file required : unistd.h
Syntax:
The function returns the absolute path to the current working directory. The function itself mallocs for an array big enough to hold the current directory name, thus we don't need to do malloc in the program. This function returns the value of the environment variable PWD, thus if it is not set the function might fail to return the current value.
Here is a program which uses get_current_dir_name to get the current working directory.
Save the file as get_current_dir.c. Compile and execute it.
3. The third way of getting the current working directory is using the command "pwd" along with the function "system". The system function executes which ever command is passed to it and prints the result on the screen. Thus we can execute the command system and pass the command "pwd" as the argument, the result of which will be the current working directory being printed on the screen.
The following program shows how the same can be implemented.
Save the file as get_system_pwd.c. Compile and execute the same.
No comments:
Post a Comment