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

file-4.26.tar.gz             这是包  可以从
http://www.sfr-fresh.com/unix/misc/file-4.26.tar.gz/ 
 网上下载
可以确定一个文件的类型
但要安装上面的包




// example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <magic.h>
#include <iostream>

#include <QDebug>
#include <QtGui>
#include <QtCore>

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() <<"filePath=" << fileInfo.filePath();
     
       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);
   }

}


















你可能感兴趣的:(linux,struct,File,null,Path)