当我这样做时:
FILE * fp = fopen("filename", "r");`
如何知道文件指针fp指向的是文件还是目录?因为我认为这两种情况下 fp 都不会为空。我能做什么?
环境是UNIX。
最佳答案
我在附近找到了这个:
#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]);
}
}
关于c - 如何区分文件指针指向的是文件还是目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18711640/