确定C文件类型 [英] Determine file type in C

查看:187
本文介绍了确定C文件类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作,显示psented作为参数传递给该程序的文件$ P $的文件类型在Linux机器上在C程序。程序需要确定文件是下列的任一项:目录,装置(常规)文件,链接,插座,或FIFO。我不完全知道如何判断文件类型。

I am working on a program in C on a linux machine that displays the file type of a file presented as an argument to the program. The program needs to determine if a file is any one of the following: directory, device, (regular) file, link, socket, or fifo. I am not exactly sure how to determine file type.

下面是我的code迄今(不要太多):

Here is my code thus far (not much):

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

    return  0;
}

谢谢!

推荐答案

使用POSIX的统计功能和阅读 ST_MODE 结构 struct stat中函数返回的领域。

Use the POSIX stat function and read the st_mode field of the structure struct stat returned by the function.

统计功能:

<一个href=\"http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html\">http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html

结构 struct stat中键入

<一个href=\"http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html\">http://pubs.opengroup.org/onlinepubs/7908799/xsh/sysstat.h.html

有关glibc的,你也可以阅读第 14.9.3测试文件中的类型的glibc 手动:

For glibc, you can also read the section 14.9.3 Testing the Type of a File of the glibc manual:

<一个href=\"http://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html\">http://www.gnu.org/software/libc/manual/html_node/Testing-File-Type.html

这篇关于确定C文件类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆