#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
struct stat file_attr;
if (stat(argv[0], &file_attr) == -1)
{
perror("stat failure");
return -1;
}
std::string file_attr_name;
if (S_ISDIR(file_attr.st_mode))
{
file_attr_name = "file is dir";
}
else if (S_ISREG(file_attr.st_mode))
{
file_attr_name = "file is normal";
}
else if (S_ISBLK(file_attr.st_mode))
{
file_attr_name = "file is block device";
}
else if (S_ISCHR(file_attr.st_mode))
{
file_attr_name = "file is char device";
}
else if (S_ISFIFO(file_attr.st_mode))
{
file_attr_name = "file is FIFO";
}
else if (S_ISSOCK(file_attr.st_mode))
{
file_attr_name = "file is socket";
}
else if (S_ISLNK(file_attr.st_mode))
{
file_attr_name = "file is link";
}
else
{
file_attr_name = "unkown file type";
}
std::cout<<file_attr_name<<std::endl;
return 0;
}
#include <fcntl.h>
#include <errno.h>
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
struct stat file_attr;
if (stat(argv[0], &file_attr) == -1)
{
perror("stat failure");
return -1;
}
std::string file_attr_name;
if (S_ISDIR(file_attr.st_mode))
{
file_attr_name = "file is dir";
}
else if (S_ISREG(file_attr.st_mode))
{
file_attr_name = "file is normal";
}
else if (S_ISBLK(file_attr.st_mode))
{
file_attr_name = "file is block device";
}
else if (S_ISCHR(file_attr.st_mode))
{
file_attr_name = "file is char device";
}
else if (S_ISFIFO(file_attr.st_mode))
{
file_attr_name = "file is FIFO";
}
else if (S_ISSOCK(file_attr.st_mode))
{
file_attr_name = "file is socket";
}
else if (S_ISLNK(file_attr.st_mode))
{
file_attr_name = "file is link";
}
else
{
file_attr_name = "unkown file type";
}
std::cout<<file_attr_name<<std::endl;
return 0;
}
本文介绍了一个使用C++编写的程序,该程序通过调用stat函数来判断文件类型,并根据不同类型的文件输出相应的信息。支持判断目录、常规文件、块设备文件、字符设备文件、FIFO、套接字及符号链接。
220

被折叠的 条评论
为什么被折叠?



