Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
474 views
in Technique[技术] by (71.8m points)

posix - Example of realpath function in C

I'm looking for an example of how to use the realpath function in a C program. I can't seem to find one on the web or in any of my C programming books.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The realpath() function is not described in the C Standard. It is however described by POSIX 1997 and POSIX 2008. If that is what you mean, here is an example:

#include <limits.h> /* PATH_MAX */
#include <stdio.h>
#include <stdlib.h>
int main(void) {
    char buf[PATH_MAX]; /* PATH_MAX incudes the  so +1 is not required */
    char *res = realpath("this_source.c", buf);
    if (res) {
        printf("This source is at %s.
", buf);
    } else {
        perror("realpath");
        exit(EXIT_FAILURE);
    }
    return 0;
}

PATH_MAX is defined in <limits.h> (<limits.h> from POSIX 1997)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...