2009-05-22 57 views

回答

8

有一个fstat(2)函数。

NAME 统计,FSTAT,LSTAT - 获取文件状态

提要

#include <sys/types.h> 
    #include <sys/stat.h> 
    #include <unistd.h> 

    int fstat(int fd, struct stat *buf); 

你可以通过调用fileno(3)得到FD。

然后你可以打电话S_ISFIFO(buf)弄明白。

+2

可能值得一提:`S_ISFIFO(buf.st_mode)`这个宏不会为您抓取结构。 – 2012-04-04 21:29:12

3

使用fstat()函数。但是,您需要使用fileno()宏从文件FILE结构中获取文件描述符。

#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <unistd.h> 

FILE *fp = fopen(path, "r"); 
int fd = fileno(fp); 
struct stat statbuf; 

fstat(fd, &statbuf); 

/* a decoding case statement would be good here */ 
printf("%s is file type %08o\n", path, (statbuf.st_mode & 0777000); 
+2

这是一个很好的例子,但对于一个没有经验的编码器来说,这是没有意义的。问答应始终归结为基本问题,以便其他具有类似问题的人可以在类似的背景下理解答案。问题是区分unix中的管道和文件。你的回答只是显示如何解析统计模式。这是一个很好的例子,你没有正确回答这个问题。这个问题的答案是`FILE * fp = fopen(path,“r”); int fd = fileno(fp); struct stat statbuf; fstat(fd,&statbuf);如果(S_ISFIFO(statbuf.st_mode))//它的管道!` – 2012-04-04 22:04:24