linux判断文件类型的多种方法

在Linux下,如果想判断一个文件的类型,可以这样写。

1.使用系统函数
cpp 复制代码
#include <iostream>
#include <sys/stat.h>

bool is_txt_file(const std::string& path)
{
    struct stat its_stat;
    if (stat(path.c_str(), &its_stat) == 0)     //是否存在
    {
        if (its_stat.st_mode & S_IFREG)
        {
            std::cout<<"file "<<path<<" is txt file\n";
            return true;
        }
        else 
        {
            std::cout<<"file "<<path<<" is not txt file\n";
            return true;
        }
    }
    std::cout<<"file "<<path<<" is not exist\n";
            
    return false;
}

int main()
{
    std::string path;
    std::cout<<"input a file path to judge if it exist:";
    std::cin>>path;
    is_txt_file(path);
    
    
    return false;
}

编译输出的效果为:

g++ -o cppexist cppexist.cpp ./cppexist

input a file path to judge if it exist:cppexist

file cppexist is txt file
$ ./cppexist

input a file path to judge if it exist:22

file 22 is not exist
$ ./cppexist

input a file path to judge if it exist:/usr/bin

file /usr/bin is not txt file

其中if (stat(path.c_str(), &its_stat) == 0)这句判断文件是否存在,

if (its_stat.st_mode & S_IFREG)这句判断文件的类型。

S_IFREG:普通文件,类似的标志还有,可用来判断不同的文件类型。

|----------|------|
| S_IFREG | 文本文件 |
| S_IFDIR | 目录 |
| S_IFLNK | 符号链接 |
| S_IFCHR | 字符设备 |
| S_IFBLK | 块设备 |
| S_IFIFO | 管道文件 |
| S_IFSOCK | 套接字 |

上面的函数可进行扩充,从而判断出具体的文件类型。

2.使用C++17中的函数

随着C++的发展,它也增加了判断文件类型的函数。

cpp 复制代码
#include <filesystem>
void cpp_file_type(const std::string &path)
{
    try{
        auto status = std::filesystem::status(path);
        if (std::filesystem::is_regular_file(status) || status.type() == std::filesystem::file_type::regular)
        {
            std::cout<<path<<" is txt file\n";
        }
    }catch (const std::filesystem::filesystem_error &e)
    {
        std::cout<<e.what()<<std::endl;
    }
}

$ ./cppexist

input a file path to judge if it exist:cppexist

file cppexist is txt file

cppexist is txt file

上面是判断文本文件的方法,

方法1:std::filesystem::is_regular_file(status)这是使用函数判断。

方法2:status.type() == std::filesystem::file_type::regular这是使用类型值判断。

它还有其他函数判断不同文件类型。

|--------------------------------------------------------------------------------------------------------------|----------------------|------|
| is_regular_file() | file_type::regular | 文本文件 |
| is_directory() | file_type::directory | 目录 |
| is_block_file() | file_type::block | 块设备 |
| is_character_file() | file_type::character | 字符设备 |
| is_fifo() | file_type::fifo | 管道文件 |
| is_socket() | file_type::socket | 套接字 |
| is_symlink() | file_type::symlink | 符号链接 |
| exists() | - | 是否存在 |

总结

通过一个文本判断函数,引申出不同类型的判断。可以使用linux的系统函数来判断文件类型,也可以使用C++的函数来判断文件类型。

相关推荐
chushiyunen32 分钟前
linux环境部署php、php-npm
linux·npm·php
草莓熊Lotso34 分钟前
【Linux网络】深入理解 HTTP 协议(四):完善 C++ HTTP 服务器:从协议原理到生产级实现
linux·运维·服务器·c语言·网络·c++·http
sulikey34 分钟前
个人Linux操作系统学习笔记7 - 进程理解
linux·笔记·学习·操作系统·进程·pid
牛油果子哥q36 分钟前
【C++前置声明与头文件】C++前置声明与头文件深度精讲:重复包含、循环依赖、重复定义报错、工程编译架构与实战解决方案
开发语言·c++
少司府37 分钟前
C++进阶:map和set的使用
开发语言·数据结构·c++·容器·stl·set·map
程序喵大人39 分钟前
C++ 程序员转型 AI Infra 学习路线
c++·人工智能·学习·ai infra
cpp_250141 分钟前
P11375 [GESP202412 六级] 树上游走
数据结构·c++·算法·题解·洛谷·树形结构·gesp六级
此生决int44 分钟前
算法从入门到精通——字符串
数据结构·c++·算法·蓝桥杯
basketball6161 小时前
设计模式入门:7. 策略模式详解 C++实现
c++·设计模式·策略模式
Je1lyfish1 小时前
CMU15-445 (2025 Fall/2026 Spring) Project#4 - Concurrency Control
开发语言·数据库·c++·笔记·后端·算法·系统架构