C++: How to check type of files without extension
Use libmagic
.
Libmagic is available on all major platforms (and many minors).
#include <boost/filesystem.hpp>
#include <boost/range.hpp>
#include <iostream>
#include <magic.h>
using namespace boost;
namespace fs = filesystem;
int main() {
auto handle = ::magic_open(MAGIC_NONE|MAGIC_COMPRESS);
::magic_load(handle, NULL);
for (fs::directory_entry const& x : make_iterator_range(fs::directory_iterator("."), {})) {
auto type = ::magic_file(handle, x.path().native().c_str());
std::cout << x.path() << "\t" << (type? type : "UNKOWN") << "\n";
}
::magic_close(handle);
}
Prints, e.g.
sehe@desktop:~/custom/boost/status$ /tmp/test
"./Jamfile.v2" ASCII text
"./explicit-failures.xsd" XML document text
"./expected_results.xml" XML document text
"./explicit-failures-markup.xml" XML document text
You can use the flags to control the detail of classification, e.g. MAGIC_MIME:
sehe@desktop:~/custom/boost/status$ /tmp/test
"./Jamfile.v2" text/plain; charset=us-ascii
"./explicit-failures.xsd" application/xml; charset=us-ascii
"./expected_results.xml" application/xml; charset=us-ascii
"./explicit-failures-markup.xml" application/xml; charset=utf-8
Or loading just /etc/magic
:
sehe@desktop:~/custom/boost/status$ /tmp/test
"./Jamfile.v2" ASCII text
"./explicit-failures.xsd" ASCII text
"./expected_results.xml" ASCII text, with very long lines
"./explicit-failures-markup.xml" UTF-8 Unicode text
Getting file extension in C
const char *get_filename_ext(const char *filename) {
const char *dot = strrchr(filename, '.');
if(!dot || dot == filename) return "";
return dot + 1;
}
printf("%s\n", get_filename_ext("test.tiff"));
printf("%s\n", get_filename_ext("test.blah.tiff"));
printf("%s\n", get_filename_ext("test."));
printf("%s\n", get_filename_ext("test"));
printf("%s\n", get_filename_ext("..."));
determine file type of a file without extension
You can use vim this way:
vim -c ':silent execute ":!echo " . &ft . " > /dev/stdout"' -c ':q!' the_file
It simply constructs command to run in the shell as a string concatenation.
How do you check a file type when there is no extension in c#
I've heard of reading the first few bytes of a file's contents and making an educated guess at the file's format. This link seems promising:
Using .NET, how can you find the mime type of a file based on the file signature not the extension
How can I check if a file is an image file without checking the path extension?
UIImage *image = [UIImage imageNamed:@"SomeFile.xyz"];
if (image == nil) {
NSLog(@"This is probably text file");
}
If image is nil means the file may be text or another type of file.
How to detect .o / .a / compiled object /executable c/c++ without extension? (to then issue 'what' on file to see what it was compiled with)
If you're in Unix/Linux, the file
command is useful for determining file type of files without relying on a file extension.
It looks at things like "is this a special device rather than a normal file", then looks for "magic numbers" which identify certain file format, etc.
Related Topics
C++ Makefile on Linux with Multiple *.Cpp Files
What Does the |= Operator Mean in C++
How to Use a Constexpr Value in a Lambda Without Capturing It
Change Terminal Font Size with C++
C and C++ Functions Without a Return Statement
How to Find The Full Path of The C++ Linux Program from Within
Are There in X86 Any Instructions to Accelerate Sha (Sha1/2/256/512) Encoding
How to Embed Node.Js Interpreter into C/C++
Why Does This Work: Returning C String Literal from Std::String Function and Calling C_Str()
Undefined Reference to Symbol '_Zn5Boost6System15System_Categoryev' Error
Initialising Reference in Constructor C++
Is the Result of a Cast an Rvalue
_Attribute_((Constructor)) Call Order Confusion
Lnk2022 Metadata Operation: Inconsistent Layout Information in Duplicated Types