Thread: I need to know category of file.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    3

    I need to know category of file.

    I need to know category of file such as picture, music, video, text etc.
    It has code to find this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    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?

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Quote Originally Posted by laserlight View Post
    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.
    In first, I use stat function but It does not tell categories. So I want to know to find category of file in order that I keep files category in database so that I select file to show in my application.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by brilliantsompra
    So I want to know to find category of file in order that I keep files category in database so that I select file to show in my application.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    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

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    6
    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?

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    3
    Quote Originally Posted by anduril462 View Post
    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.
    Thank you everybody.
    I find the solution of this problem but I try overall to test it.
    The code is below.

    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;
    }
    
    I change a name of jpeg file to other type but also this program is also tell that the file is image/jpeg.

    Reference : libmagic/magic.h library tutorial with example. (It's magic!) | vivithemage's blog

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I post help w/ OpenGL set up in VS in this forum category?
    By monkey_c_monkey in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2012, 08:21 PM
  2. get keyword and category of a page
    By Checker1977 in forum Tech Board
    Replies: 11
    Last Post: 12-27-2008, 07:23 AM
  3. category of Math signs???
    By Vorkosigan in forum C++ Programming
    Replies: 2
    Last Post: 01-22-2004, 05:00 PM
  4. Algorithm for building category and LDAP membership
    By gogo in forum C# Programming
    Replies: 0
    Last Post: 07-09-2003, 04:58 AM