我正在 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 stat
的st_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/