如何区分文件指针指向文件或目录?

lkk*_*ing 5 c unix fopen file

当我做:

FILE * fp = fopen("filename", "r");`  
Run Code Online (Sandbox Code Playgroud)

我怎么知道文件指针fp指向文件或目录?因为我认为两种情况下fp都不会为空.我能做什么?

环境是UNIX.

No *_*ame 3

我在附近发现了这个:

#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
    int status;
    struct stat st_buf;

    status = stat ("your path", &st_buf);
    if (status != 0) {
        printf ("Error, errno = %d\n", errno);
        return 1;
    }

    // Tell us what it is then exit.

    if (S_ISREG (st_buf.st_mode)) {
        printf ("%s is a regular file.\n", argv[1]);
    }
    if (S_ISDIR (st_buf.st_mode)) {
        printf ("%s is a directory.\n", argv[1]);
    }
}
Run Code Online (Sandbox Code Playgroud)