Example of realpath function in C

前端 未结 4 830
忘掉有多难
忘掉有多难 2020-12-09 17:49

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.

4条回答
  •  感情败类
    2020-12-09 18:15

    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  /* PATH_MAX */
    #include 
    #include 
    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) {
            printf("This source is at %s.\n", buf);
        } else {
            perror("realpath");
            exit(EXIT_FAILURE);
        }
        return 0;
    }
    

    PATH_MAX is defined in ( from POSIX 1997)

提交回复
热议问题