2015-01-08 18 views
0

时,我不知道哪里是我在程序的问题,我用一个debuger但它没有指定在哪个行是错误段错误检测的文件扩展名

#define _POSIX_SOURCE //pour nofile 

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



int main (void) 
{ 
    const char *description; 
    magic_t cookie; 

    FILE* Texte= NULL; 
    if(NULL == (Texte = fopen("chaines", "r"))) 
    { // then fopen failed 
    perror("fopen failed for chaines for read"); 
    exit(EXIT_FAILURE); 
    } 

    // implied else, fopen successful 
    cookie = magic_open(MAGIC_NONE); 
    description = magic_descriptor(cookie, fileno(Texte)); 
    printf("%s\n", description); 
return 0; 
} 

And this is my debuger result 
    Starting program: /home/hamza/Bureau/Projet_suffixe/suffixe_db 
warning: no loadable sections found in added symbol-file system-supplied DSO at 0x7ffff7ffa000 

Program received signal SIGSEGV, Segmentation fault. 
0x00007ffff7888e41 in ??() from /lib/x86_64-linux-gnu/libc.so.6 

谢谢。

+1

重新编译调试符号。 –

+1

难道是'magic_open'在失败时返回'NULL'? –

+1

检查magic_open()是否成功。如果是这样,请检查magic_descriptor()是否成功。 – nos

回答

0

您应该检查magic_openmagic_descriptor是否成功,并通过magic_load()函数加载魔法数据库。

#define _POSIX_SOURCE //pour nofile 

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

int main (void) 
{ 
    magic_t cookie; 

    FILE* Texte= NULL; 
    if (NULL == (Texte = fopen("chaines", "r"))) 
    { // then fopen failed 
     perror("fopen failed for chaines for read"); 
     exit(EXIT_FAILURE); 
    } 

    // implied else, fopen successful 
    cookie = magic_open(MAGIC_NONE); 
    if (cookie != NULL) 
    { 
     /* you should load the database */ 
     if (magic_load(cookie, NULL) == 0) 
     { 
      const char *description; 

      description = magic_descriptor(cookie, fileno(Texte)); 
      if (description != NULL) 
       printf("%s\n", description); 
      else 
       printf("Unknown\n"); 
      magic_close(cookie); // cleanup resources 
     } 
     else 
      fprintf(stderr, "error loading the magic database\n"); 
    } 
    fclose(Texte); // cleanup resources 

    return 0; 
} 

这不会崩溃。

此外,还有一个magic_file功能,您通过cookie和文件名,你可以直接写这个

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

int main (void) 
{ 
    magic_t cookie; 

    cookie = magic_open(MAGIC_NONE); 
    if (cookie != NULL) 
    { 
     /* you should load the database */ 
     if (magic_load(cookie, NULL) == 0) 
     { 
      const char *description; 

      description = magic_file(cookie, "chaines"); 
      if (description != NULL) 
       printf("%s\n", description); 
      else 
       printf("Unknown\n"); 
      magic_close(cookie); 
     } 
     else 
      fprintf(stderr, "error loading the magic database\n"); 
    } 
    return 0; 
} 
+0

谢谢。它表明我不知道这是meas_descreption有问题 – user3568611

+0

它从来没有返回我的情况。可能是'mime'数据库有问题或什么的。我不知道'libmagic'如何在内部工作。 –

+0

可能是你havnt添加正确的?我在这两种情况下,它显示我未知但我找不到如何解决它..我有一个remarq:magic_t是在我的程序中不是绿色(或蓝色)黑色 – user3568611