识别linux某个文件的类型,linux 下从文件内容来识别一个文件类型的实例

file-4.26.tar.gz             这是包  可以从

http://www.sfr-fresh.com/unix/misc/file-4.26.tar.gz/

网上下载

可以确定一个文件的类型

但要安装上面的包

// example

#include

#include

#include

#include

#include

#include

#include

#include

using namespace std;

void type(const QString &path);

int main(int argc,char* argv[])

{

/*   struct magic_set *pms = NULL;

// const char* result = NULL;

// const char* filePath = NULL;

pms = magic_open(MAGIC_MIME_TYPE);

if (pms == NULL)

{

(void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory/n");

return 0;

}

if (magic_load(pms, NULL) == -1)             //***************

{

(void)fprintf(stderr, "ERROR loading with NULL file: %s/n", magic_error(pms));

}

if(argc==2 && argv[1]!=NULL)

{

if ((result = magic_file(pms, argv[1])) == NULL)     //***************

{

(void)fprintf(stderr, "ERROR loading file %s: %s/n",argv[1],magic_error(pms));

}

(void)printf("%s: %s/n", argv[1], result);

}

else

{

puts("USAGE---run file----testing filePath/n");

}

*/

type("/scratchbox/media/mmc2/music");

return 0;

}

void type(const QString &path)

{

struct magic_set *ms = NULL;

// const char* result = NULL;

// const char* filePath = NULL;

ms = magic_open(MAGIC_MIME);

if (ms == NULL)

{

(void)fprintf(stderr, "ERROR opening MAGIC_NONE: out of memory/n");

return;

}

if (magic_load(ms, NULL) == -1)             //***************

{

(void)fprintf(stderr, "ERROR loading with NULL file: %s/n", magic_error(ms));

}

//*************************

QDir dir(path);

const char* result = NULL;

foreach (QFileInfo fileInfo,dir.entryInfoList(QDir::Files))

{

qDebug() <

if(ms)

{

if ((result = magic_file(ms, fileInfo.filePath().toStdString ().c_str() )) == NULL)

{

(void)fprintf(stderr, "ERROR loading file %s/n",magic_error(ms));

}

(void)printf("%s/n",result);

printf("/n");

}

}

foreach (QString subDir,dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot))

{

type(path + QDir::separator() + subDir);

}

if(ms)

{

magic_close(ms);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值