试图用/有/编译第三方库,libmagic。 C / C ++的文件类型检测 [英] Trying to use/include/compile 3rd party library, libmagic. C/C++ filetype detection

查看:2097
本文介绍了试图用/有/编译第三方库,libmagic。 C / C ++的文件类型检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在寻找一种方式来检测文件流的文件类型后,我发现了Unix file命令使用libmagic,我试图使利用图书馆自己,但我不能得到它的工作。我很少集成在自己的第三方code,所以这可能是我的问题的一个很大的一部分。

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.

为什么:

我这样做是因为我有一个便携式图形用户界面的图像浏览应用程序,将需要检测从给定的文件名,然后将图像文件类型内部存档的文件类型(RAR,ZIP,更多?)。我希望我可以使用libmagic Windows和Linux(和Mac),所以如果不是这种情况下,阻止我现在B / C我需要找到别的东西。

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.

尝试:

我发现<一个href=\"http://www.linuxquestions.org/questions/programming-9/c-is-there-a-way-to-determine-the-file-type-of-a-given-file-626280/\"相对=nofollow>有人做类似的东西,但我不能跟着他们在做什么,我已经不知道该如何编译所有/运行任何开始瞎搞。

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.

编辑:现在我有

#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;
}

与编译:

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

工作。我不得不去突触,虽然安装libmagic-dev的。我得测试,看看我是否可以复制到/usr/lib/libmagic.a我的源目录编制我在Windows上的应用程序MingW平台的时候,但是这会成为一个问题后,我想。

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.

推荐答案

__ FILE __ 是用于调试/日志记录保留pre-处理符号宏。认为这是一个例子:

__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);
}

如果调用的malloc 失败,你会看到在这样上面的例子中输出:

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

注意如何code拿起原始出处code。上面的例子说明了这个用法。

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

我觉得你的code应该是这样的:

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;
}

在code以上检查是否有传递到这个程序参数,该参数是一个文件名,如的argv [0] 指向可执行文件名(编译的二进制),的argv [1] 指向字符指示有问题的文件名数组(字符串)。

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.

要编译:


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

编辑: 感谢阿洛克在这里指出了错误...

Thanks Alok for pointing out the error here...

如果您不能确定libmagic居住的地方,寻找它位于/ usr / local / lib目录和/ usr /本地/包括 - 这取决于你的安装

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.

请参阅此找到predefined宏这里

See this to find the predefined macros here.

希望这有助于
诚挚的问候,
汤姆。

Hope this helps, Best regards, Tom.

这篇关于试图用/有/编译第三方库,libmagic。 C / C ++的文件类型检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆