Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 553369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:35:52+00:00 2026-05-13T11:35:52+00:00

After looking for a way to detect the filetype of a file stream, I

  • 0

After looking for a way to detect the filetype of a file stream, I found that the Unix file command uses libmagic and I’m trying to make use of the library myself, but I can’t get it to work. I’ve rarely integrated 3rd party code in my own, so that’s probably a big part of my problem as well.

Why:

I’m doing this because I have a portable gui image viewing app that will need to detect archive file types (rar, zip, more?) from given filename and then the image file types inside. I’m hoping that I can use libmagic for Windows and Linux (and Mac), so if this isn’t the case, stop me now b/c I’ll need to find something else.

Attempt:

I found somebody doing something similar, but I can’t follow what they’re doing, and I’ve no idea how compile/run anything at all to start messing around.

My first instinct was to do something like:

// fileTypeTest.cpp, placed in file-5.03/src/ (source from link above)
#include <stdio.h>
#include "magic.h"
int main() {
  magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(myt,NULL);
  printf("magic output: '%s'\n",magic_file(myt,__FILE__));
  magic_close(myt);

  return 0;
}

then compile with something like:

$ gcc magic.c -o magic.o
$ g++ fileTypeTest.cpp -o fileTypeTest magic.o

which (obviously?) doesn’t work. I don’t even know where to start looking, what questions to ask, or if this is the right direction to go to solve my original problem in the first place.


Edit: Now I have

#include    <stdio.h>
#include    <magic.h>

int main(int argc, char* argv[]) {
  if (argc != 2) {
    printf("bad arguments");
    return 0;
  }
  magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
  magic_load(myt,NULL);
  printf("magic output: '%s'\n", magic_file(myt, argv[1]));
  magic_close(myt);

  return 0;
}

compiling with:

$ g++ -L/usr/lib -libmagic fileTypeTest.cpp -o fileTypeTest

works. I had to go to synaptic and install libmagic-dev though. I’ll have to test to see if I can just copy /usr/lib/libmagic.a into my source directory when compiling my app on Windows’s MingW, but that’ll be for another question later, I suppose.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:35:52+00:00Added an answer on May 13, 2026 at 11:35 am

    __FILE__ is a reserved pre-processing symbol macro used for debugging/logging purposes. Consider this as an example:

    // This file is called test.c
    char *p = NULL;
    if (!(p = malloc((1 * sizeof(char) + 1)))){
       printf("Error in file: %s @ line %d\n\tMalloc failed\n", __FILE__, __LINE__);
       exit(-1);
    }
    

    If the call to malloc failed you will see the output in the above example like this:

    Error in file: test.c @ line 23
           Malloc failed
    

    Notice how the code picks up the original source code. The above example illustrates the usage of this.

    I think your code should be something like this:

    // fileTypeTest.cpp, placed in file-5.03/src/ (source from link above)
    #include <stdio.h>
    #include "magic.h"
    int main(int argc, char **argv) {
      if (argc > 1){
         magic_t myt = magic_open(MAGIC_CONTINUE|MAGIC_ERROR/*|MAGIC_DEBUG*/|MAGIC_MIME);
         magic_load(myt,NULL);
         printf("magic output: '%s'\n",magic_file(myt,argv[1]));
         magic_close(myt);
      }
      return 0;
    }
    

    The code above checks if there is a parameter that is passed into this program and the parameter would be a filename, i.e. argv[0] points to the executable name (the compiled binary), argv[1] points to the array of chars (a string) indicating the filename in question.

    To compile it:

    g++ -I/usr/include -L/usr/lib/libmagic.so  fileTestType.cpp -o fileTestType
    g++ -L/usr/lib -lmagic fileTestType.cpp -o fileTestType
    

    Edit: Thanks Alok for pointing out the error here…

    If you are not sure where the libmagic reside, look for it in the /usr/local/lib, and /usr/local/include – this depends on your installation.

    See this to find the predefined macros here.

    Hope this helps,
    Best regards,
    Tom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking after a way to AlphaBlend a child form, if possible using layered
im looking for a way to print the array as is after each pass.
I'm looking for a way to reboot my app after an in app purchase
I were looking around for a way to Indent my logging statements. After a
After looking at RescueTime for windows/mac, it seems that there's a version for linux
Hmm, sounds easy enough but after looking at the ones that come with StringTemplate,
I find it strange that after looking everywhere I don't find any tool to
After looking for several hours and not finding an answer either way I decided
I'm looking for a javascript/ajax based file upload. The way it would work is
I'm looking if -glkView:drawInRect: can detect that it's drawing for -snapshot method, rather than

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.