簡體   English   中英

我如何知道使用 Boost.Filesystem 的文件類型?

[英]How can I know the type of a file using Boost.Filesystem?

我正在使用 Boost,但我無法在安裝目錄或 Web 中找到有關文件系統庫的完整(或好的)文檔。 我發現的“-ls”示例非常有用,但這還不夠。

提前致謝 :)

下面是一個例子:

#include <iostream>
#include <boost/filesystem.hpp>
#include <string>

using namespace std;

int main() {
    string filename = "hello.txt";

    string extension = boost::filesystem::extension(filename);

    cout << "filename extension: " << extension << endl;

    return 0;    
}

輸出是“.txt”

提醒:使用“-lboost_system -lboost_filesystem”編譯

怎么樣:

http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/index.htm

用於確定文件類型(目錄、普通文件等)的函數可在此子頁面上找到: http : //www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#file_status

如果您正在尋找文件擴展名,請查看: template <class Path> typename Path::string_type extension(const Path &p); 在頁面上: http : //www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#Convenience-functions

以下是如何從文件中獲取擴展名的示例:

std::vector<boost::filesystem::path> GetAllFileExtensions()
{
    std::vector<boost::filesystem::path> fileExtensions;
    boost::filesystem::directory_iterator b(boost::filesystem::current_path()), e;
    for (auto i=b; i!=e; ++i)
    {
        boost::filesystem::path fe = i->path().extension();
        std::cout << fe.string() << std::endl;
        fileExtensions.push_back(fe);
    }

    return fileExtensions;
}

std::vector<boost::filesystem::path> fileExtensions = GetAllFileExtensions();

此示例僅獲取所有文件並從中刪除擴展名並顯示在標准輸出上,您可以修改函數 GetAllFileExtensions 以僅查看一個文件

我不知道您使用的是哪種操作系統,但在像 GNU/Linux 這樣的 UN*X 類型系統上,文件擴展名只是文件名的一部分,並且確實對文件內容進行了任何聲明。 通常它會被一起忽略,只檢查文件的 MIME 類型(由像 Nautilus 這樣的文件管理器完成)。

使用 Boost 您不能 (?) 獲得文件的 MIME 類型,但您可以使用libmagicfile實用程序也使用它)。 它是一個純 C 庫,但函數和類型可以很容易地包裝在一些 RAII 類中。

#include <iostream>
#include <string>

#include <cassert>
#include <magic.h>

int main()
{
  std::string filename{"test.png"};

  // allocate magic cookie
  magic_t magic;
  assert( (magic = magic_open(MAGIC_MIME_TYPE)) != nullptr );

  // load the default magic database (indicated by nullptr)
  assert( magic_load(magic, nullptr) == 0 );

  // compile the default magic database (indicated by nullptr)
  assert( magic_compile(magic, nullptr) == 0 );

  // get description of the filename argument
  char const * mime;
  assert( (mime = magic_file(magic, filename.c_str())) != nullptr );

  std::cout << filename << " has type " << mime << "\n";

  // free magic cookie (BEWARE! this frees "mime")
  magic_close(magic);
}

在我的系統上,文件test.png存在並且程序打印

test.png has type application/octet-stream

當然不完美(我期望image/png )但足夠接近。

用這個小代碼片段來檢測沒有魔法 lib/Qt 依賴的 mimetype: https : //github.com/drodil/cpp-util/blob/master/file/mime/detector.hpp

也許你可以重用它。

您可以使用 libmagic,類似於之前的答案,但是您不需要編譯。 libmagic 幾乎可以檢測任何文件類型:

#include <iostream>
#include <string>
#include <boost/filesystem.hpp>
#include <magic.h>

using namespace std;
namespace bfs = boost::filesystem;

class MagicFile {
  magic_t mag;

public:
  MagicFile() {
    mag = magic_open(MAGIC_MIME_TYPE);
    if (mag == nullptr)
      throw runtime_error("Magic error");
    magic_load(mag, NULL);
  }

  ~MagicFile() {
    magic_close(mag);
  }

  string mime(bfs::path p) {
    return magic_file(mag, p.c_str());
  }
};

使用代碼:

cout << "mime type: " << MagicFile().mime("myfile.dat") << endl;

或者:

MagicFile magic;
cout << "mime type: " << magic.mime("myfile.dat") << endl;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM