我不知道我的程序中的问题在哪里,我使用了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
谢谢。
答案 0 :(得分:0)
您应该检查magic_open
和magic_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;
}