如何检查文件是否是C ++中的常规文件?

时间:2021-07-11 07:21:03

How do i check in C++ if a file is a regular file (and is not a directory, a pipe, etc.)? I need a function isFile().

如果文件是常规文件(并且不是目录,管道等),我如何签入C ++?我需要一个函数isFile()。

DIR *dp;
struct dirent *dirp;

while ((dirp = readdir(dp)) != NULL) {
if ( isFile(dirp)) {
     cout << "IS A FILE!" << endl;
i++;
}

I've tried comparing dirp->d_type with (unsigned char)0x8, but it seems not portable through differents systems.

我已经尝试将dirp-> d_type与(unsigned char)0x8进行比较,但它似乎无法通过不同系统进行移植。

6 个解决方案

#1


3  

You need to call stat(2) on the file, and then use the S_ISREG macro on st_mode.

您需要在文件上调用stat(2),然后在st_mode上使用S_ISREG宏。

#2


23  

You can use the portable boost::filesystem (The standard C++ library could not have done this up until recent introduction of std::filesystem in C++17):

您可以使用可移植的boost :: filesystem(标准C ++库在最近在C ++ 17中引入std :: filesystem之前无法做到这一点):

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>

int main() {
    using namespace boost::filesystem;

    path p("/bin/bash");
    if(is_regular_file(p)) {
        std::cout << "exists and is regular file" << std::endl;
    }
}

#3


3  

C++ itself doesn't deal with file systems, so there's no portable way in the language itself. Platform-specific examples are stat for *nix (as already noted by Martin v. Löwis) and GetFileAttributes for Windows.

C ++本身并不处理文件系统,因此语言本身没有可移植的方式。特定于平台的示例是* nix的统计信息(如Martinv.Löwis所述)和Windows的GetFileAttributes。

Also, if you're not allergic to Boost, there's fairly cross-platform boost::filesystem.

另外,如果你对Boost没有过敏,那就是相当跨平台的boost :: filesystem。

#4


1  

In C++17, you may use std::filesystem::is_regular_file

在C ++ 17中,您可以使用std :: filesystem :: is_regular_file

#include <filesystem> // additional include

if(std::filesystem::is_regular_file(yourFilePathToCheck)) 
    ; //Do what you need to do

Note that earlier version of C++ may have had it under std::experimental::filesystem (Source: http://en.cppreference.com/w/cpp/filesystem/is_regular_file)

请注意,早期版本的C ++可能已经在std :: experimental :: filesystem下使用了它(来源:http://en.cppreference.com/w/cpp/filesystem/is_regular_file)

#5


0  

Thank you all for the help, i've tried with

谢谢大家的帮助,我试过了

while ((dirp = readdir(dp)) != NULL) { 
   if (!S_ISDIR(dirp->d_type)) { 
        ... 
        i++; 
   } 
} 

And it works fine. =)

它工作正常。 =)

#6


0  

#include <boost/filesystem.hpp>

bool isFile(std::string filepath)
{
    boost::filesystem::path p(filepath);
    if(boost::filesystem::is_regular_file(p)) {
        return true;
    }
    std::cout<<filepath<<" file does not exist and is not a regular file"<<std::endl;
    return false;
}

#1


3  

You need to call stat(2) on the file, and then use the S_ISREG macro on st_mode.

您需要在文件上调用stat(2),然后在st_mode上使用S_ISREG宏。

#2


23  

You can use the portable boost::filesystem (The standard C++ library could not have done this up until recent introduction of std::filesystem in C++17):

您可以使用可移植的boost :: filesystem(标准C ++库在最近在C ++ 17中引入std :: filesystem之前无法做到这一点):

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>

int main() {
    using namespace boost::filesystem;

    path p("/bin/bash");
    if(is_regular_file(p)) {
        std::cout << "exists and is regular file" << std::endl;
    }
}

#3


3  

C++ itself doesn't deal with file systems, so there's no portable way in the language itself. Platform-specific examples are stat for *nix (as already noted by Martin v. Löwis) and GetFileAttributes for Windows.

C ++本身并不处理文件系统,因此语言本身没有可移植的方式。特定于平台的示例是* nix的统计信息(如Martinv.Löwis所述)和Windows的GetFileAttributes。

Also, if you're not allergic to Boost, there's fairly cross-platform boost::filesystem.

另外,如果你对Boost没有过敏,那就是相当跨平台的boost :: filesystem。

#4


1  

In C++17, you may use std::filesystem::is_regular_file

在C ++ 17中,您可以使用std :: filesystem :: is_regular_file

#include <filesystem> // additional include

if(std::filesystem::is_regular_file(yourFilePathToCheck)) 
    ; //Do what you need to do

Note that earlier version of C++ may have had it under std::experimental::filesystem (Source: http://en.cppreference.com/w/cpp/filesystem/is_regular_file)

请注意,早期版本的C ++可能已经在std :: experimental :: filesystem下使用了它(来源:http://en.cppreference.com/w/cpp/filesystem/is_regular_file)

#5


0  

Thank you all for the help, i've tried with

谢谢大家的帮助,我试过了

while ((dirp = readdir(dp)) != NULL) { 
   if (!S_ISDIR(dirp->d_type)) { 
        ... 
        i++; 
   } 
} 

And it works fine. =)

它工作正常。 =)

#6


0  

#include <boost/filesystem.hpp>

bool isFile(std::string filepath)
{
    boost::filesystem::path p(filepath);
    if(boost::filesystem::is_regular_file(p)) {
        return true;
    }
    std::cout<<filepath<<" file does not exist and is not a regular file"<<std::endl;
    return false;
}