C++: How to Check Type of Files Without Extension

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



Leave a reply



Submit