我正在寻找有关如何在 C 程序中使用 realpath 函数的示例。我似乎无法在网上或我的任何 C 编程书籍中找到它。
最佳答案
备注
- C 标准中没有描述
realpath()
函数- 然而,它由 POSIX 1997 和 POSIX 2008 描述。
- C 标准中没有描述
示例代码
#include <limits.h> /* PATH_MAX */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
char buf[PATH_MAX]; /* PATH_MAX incudes the \0 so +1 is not required */
char *res = realpath("this_source.c", buf);
if (res) { // or: if (res != NULL)
printf("This source is at %s.\n", buf);
} else {
char* errStr = strerror(errno);
printf("error string: %s\n", errStr);
perror("realpath");
exit(EXIT_FAILURE);
}
return 0;
}
- 引用资料
- PATH_MAX 定义在 PATH_MAX 从 POSIX 1997
- errno
- strerror
关于c - C 中 realpath 函数的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1563168/