In Linux, it is estimated that you often use the PWD command, which is to print the current working path, that is, print working directroy. Today we also use the C language to implement this command.
To implement this function, you need to use the following system call:
# Include <unistd. h>
Char * getcwd (char * Buf, size_t size );
This system call returns the absolute path of the current working directory. The absolute path value is retained in the size Buf. If the buffer is too small, null is returned, and errno is set to erange, if Buf is null, the behavior is undefined. If the function is successfully called, the returned value is Buf. If the call fails, null is returned. For the cause of failure, see errno.
In addition, a function is also used:
# Include <string. h>
Char * strerror (INT errnum );
This function converts the error code errnum into an error description string and returns it.
The program is simple and the complete code is as follows:
Download: Pwd. c
1./* Pwd. C */
2. # include <unistd. h>
3. # include <stdio. h>
4. # include <string. h>
5. # include <errno. h>
6.
7. # define buf_siz 2048
8. Int main ()
9 .{
10. Char Buf [buf_siz];
11.
12. If (getcwd (BUF, buf_siz ))
13. printf ("% s/n", Buf );
14. Else
15. fprintf (stderr, "error occured: % s", strerror (errno ));
16. Exit (0 );
17 .}
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/xiaolang85/archive/2010/02/25/5326484.aspx