I need to know category of file such as picture, music, video, text etc.
It has code to find this?
I need to know category of file such as picture, music, video, text etc.
It has code to find this?
Why do you want to do this? There are many ways to categorise files, and some of them very much depend on convention that may be difficult to determine without prior knowledge.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Categorize by file extension or, if you're feeling really adventurous, create a set of file 'parsing' routines to identify the various 'signatures' common to certain file types (which can often be something of an exercise in ambiguity, mind you!). Of course, the real question is whether or not you even need such functionality in the first place. What is the context for this particular program?
If you're maintaining a database of files, then perhaps the solution is to require the user to select the file category to be saved when entering data into the database. Assuming no data entry error, this will be more reliable than trying to guess the "category" of the file.Originally Posted by brilliantsompra
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
There's no standard way of doing this, and it wont be easy, unless you use the extension and merely trust it to be correct. You didn't say one way or the other, so I don't know what OS you're using. If it's a *nix flavor, you can use the file command (file(1): determine file type - Linux man page). There's a library called libmagic (libmagic(3): Magic number recognition library - Linux man page). The util and the library are open source, so if nothing else, you can read the code and use or adapt as needed.
In general, files don't have categories. Some have an indicator at the beginning to indicate the type of file (JEPG GIF, etc) but few files really have this. Sometimes the file extension defines the category (.DAT, .DBF, .TXT) but that can easily fall apart. So as laserlight suggests, have the user enter the category.
Definition: Politics -- Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
Most file selection widget API's provide some sort of regex support for filtering file types, too. Maybe that would be a more appropriate place to implement your listing logic?
Thank you everybody.
I find the solution of this problem but I try overall to test it.
The code is below.
I change a name of jpeg file to other type but also this program is also tell that the file is image/jpeg.Code:#include <stdio.h> #include <magic.h> int main(void) { char *actual_file = "/file/you/want.yay"; const char *magic_full; magic_t magic_cookie; /*MAGIC_MIME tells magic to return a mime of the file, but you can specify different things*/ magic_cookie = magic_open(MAGIC_MIME); if (magic_cookie == NULL) { printf("unable to initialize magic library\n"); return 1; } printf("Loading default magic database\n"); if (magic_load(magic_cookie, NULL) != 0) { printf("cannot load magic database - %s\n", magic_error(magic_cookie)); magic_close(magic_cookie); return 1; } magic_full = magic_file(magic_cookie, actual_file); printf("%s\n", magic_full); magic_close(magic_cookie); return 0; }
Reference : libmagic/magic.h library tutorial with example. (It's magic!) | vivithemage's blog
Because it *IS* a jpeg file! That's the "magic" of libmagic...it doesn't work based on filename, it works based on the actual signature of the file.