确定C文件类型 [英] Determine file type in 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屋!