c - 在 C 中确定文件类型

标签 c file file-type

我正在 linux 机器上用 C 编写一个程序,它显示作为程序参数呈现的文件的文件类型。该程序需要确定文件是否是以下任何一种:目录、设备、(常规)文件、链接、套接字或 fifo。我不确定如何确定文件类型。

到目前为止,这是我的代码(不多):

int
main(int argc, char **argv)
{
    if( argc == 1 )     /* default: current directory */
        puts("Directory");
    else
        while( --argc > 0 )
            determine_ftype(*++argv);

    return  0;
}

谢谢!

最佳答案

使用POSIX stat函数,读取函数返回的结构体struct statst_mode字段。

统计函数:

http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html

结构struct stat类型:

http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html

对于glibc,您还可以阅读glibc手册的14.9.3 测试文件类型部分:

http://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html

关于c - 在 C 中确定文件类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11276521/

相关文章:

c++ - C中的MAX和MIN是什么? #定义函数

c - 错误 : file was built for unsupported file format which is not the architecture being linked (x86_64)

github - 在 GitHub 上显示二进制文件的文本差异

c - 为什么我的 3d 数组是相反的?

c - 奇怪的错误(取消引用指向不完整类型的指针)

java - Java中获取文件特定版本的图标

hadoop - 如何处理大量的 Parquet 文件

cocoa - 如何使 Finder 'Open With' 适用于我的应用程序(Xcode、OS X)?

java - Java 的 BufferedReader.readLine() 是否将整个文件放入内存?

c++ - 使用非阻塞 iNotify