C++ 我如何知道使用 Boost.Filesystem 的文件类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/959837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I know the type of a file using Boost.Filesystem?
提问by reefaktor
I'm using Boost but I cannot find complete (or good) documentation about the filesystem library in the installation directory nor the web. The "-ls" example I found has been quite a helper but it's not enough.
我正在使用 Boost,但我无法在安装目录或 Web 中找到有关文件系统库的完整(或好的)文档。我发现的“-ls”示例非常有用,但这还不够。
Thanks in advance :)
提前致谢 :)
采纳答案by Laserallan
How about:
怎么样:
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/index.htm
The functions for figuring out the file type (directory, normal file etc.) is found on this subpage: http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#file_status
用于确定文件类型(目录、普通文件等)的函数可在此子页面上找到:http: //www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#file_status
If you are looking for the file extension check out: template <class Path> typename Path::string_type extension(const Path &p);
on the page:
http://www.boost.org/doc/libs/1_39_0/libs/filesystem/doc/reference.html#Convenience-functions
如果您正在寻找文件扩展名,请查看: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
回答by Jee lee
Here is an example:
下面是一个例子:
#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;
}
The output is ".txt"
输出是“.txt”
Reminder: Compile with '-lboost_system -lboost_filesystem'
提醒:使用“-lboost_system -lboost_filesystem”编译
回答by serup
Here is an example of how you can fetch extension from files :
以下是如何从文件中获取扩展名的示例:
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();
This example just takes all files and strips extension from them and shows on standard output, you could modify function GetAllFileExtensions to only look at one file
此示例仅获取所有文件并从中删除扩展名并显示在标准输出上,您可以修改函数 GetAllFileExtensions 以仅查看一个文件
回答by Henri Menke
I don't know which operating system you use, but on the UN*X type systems like GNU/Linux the file extension is merely part of the filename and does make any statement about the file contents. Usually it is ignored all together and only the MIME type of the file is examined (which is done by file managers like Nautilus).
我不知道您使用的是哪种操作系统,但在像 GNU/Linux 这样的 UN*X 类型系统上,文件扩展名只是文件名的一部分,并且确实对文件内容进行了任何声明。通常它会被一起忽略,只检查文件的 MIME 类型(由像 Nautilus 这样的文件管理器完成)。
Using Boost you cannot (?) obtain the MIME type of a file but you can use libmagic
(which is also used by the file
utility). It is a pure C library but the functions and types could easily be wrapped in some RAII classes.
使用 Boost 您不能 (?) 获取文件的 MIME 类型,但您可以使用libmagic
(该file
实用程序也使用)。它是一个纯 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);
}
On my system the file test.png
exists and the program prints
在我的系统上文件test.png
存在并且程序打印
test.png has type application/octet-stream
Certainly not perfect (I expected image/png
) but close enough.
当然不完美(我预期image/png
)但足够接近。
回答by drodil
Did this small code snippet to detect mimetype without magic lib/Qt dependency: https://github.com/drodil/cpp-util/blob/master/file/mime/detector.hpp
用这个小代码片段来检测没有魔法 lib/Qt 依赖的 mimetype:https: //github.com/drodil/cpp-util/blob/master/file/mime/detector.hpp
Maybe you can reuse that.
也许你可以重复使用它。
回答by Alexander Kuznetsov
You can use libmagic, similar to the previous answer, however you don't need to compile. libmagic detects almost any file type:
您可以使用 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());
}
};
To use the code:
使用代码:
cout << "mime type: " << MagicFile().mime("myfile.dat") << endl;
Or:
或者:
MagicFile magic;
cout << "mime type: " << magic.mime("myfile.dat") << endl;