Hi,

I have to write a Dump Tool for a file on UNIX. I have never written a dump tool before. Can anyone please guide me how to proceed??

jazz

you will have to be a little more specific about the requirements -- a "Dump tool" can mean almost anything. My interpretation would be a program that displays the contents of database tables. Yours might be something else entirely.

A dump program takes a file as argument, reads that file and gives a text dump of the file.

I know there have been lots of those written -- google will show you a few, such as this one. It was written for dumping contents of RAM but you should be able to easily modify it for files.

Thankx that was of quite good help!!

Hi,

The Dump Tool i wrote takes a binary file as an argument , reads it and gives a txt file as an output. But there is some problem with the code can anyone help me?

FILE     *fp_inFile; 
   FILE    *fp_outFile;
   static int iGetABCFile(char*);
   static int iGetXYZFile(char*);
   char ctype; /* Two types of files can be passed as aruments file of type ABC or of type XYZ */
   char caoutfile[512]; 
     int main( int argc, char **argv ) {
                 /* Checking for correct number of arguments */ 
               if((argc == NULL)||(argc > 3)) {
               printf("\tErr -> command line\n");
               printf("\tUSAGE : DumpFile Type Filename\n");
                           exit(NG);
     }
     else {
               printf("\tCommand accepted  -> Type :[%s] FileName :(%s)\n",argv[1],argv[2]);
     }
     /* Copying the inputfile name and concatinating ".txt" to the output file - If the inputfile is ABC then the output file should be ABC.txt*/  
     strcpy(caoutfile, argv[2]);
     strcat(caoutfile,".txt");
     printf("\tNew FileName = %s\n",caoutfile);
     /* fopen */
               if(NULL==(fp_inFile=fopen(argv[2],"rb"))) {
               printf(β€œIN File Open Err! -> %s\n",argv[2]);
               exit(NG);
               } 
               if ((fp_outFile=fopen(caoutfile, "w")) == NULL) {
               printf( "OutFile Open Error\n" );
               exit( NG );
               }
                  if(strcmp(argv[1],"ABC") == 0) {
                            ctype=0;
                  }
                 else if(strcmp(argv[1],"XYZ") == 0) {
                              ctype=1;
                  }
 
               /* recognising the type of File Entered at command line.  */
               switch(ctype)
                 {
                 case 0 :
                             iGetABCFile(char*)
                            break;
               case 1 :
                             if(NULL == iGetXYZFile(char*)
                             exit(NG);
                           break;
                 default : printf("\tError -> Type must be one of the following – ABC or XYX \n");
                           break;
                 }
                  /* fclose */
                  fclose(fp_inFile);
                  fclose(fp_outFile);
         exit( OK );
   }
 /*======================================================================
        FUNCTION     : Get ABC File
        RETURN       : OK / NG
 =====================================================================*/
   int iGetABCFile(char *acABCFile)
   {
               FILE     *fp;
               int        i,j;
               unsigned long  ulRecordCount, ulDatabaseID, ulAlbumID, ulVolumeID;
               unsigned long  ulDatasetID, ulSectionID, ulLayerID, ulRecordID, ulNameCount;
               unsigned short usNameType;
               unsigned char  ucLanguageCode;
               char           acString[40];
                     if ((fp=fopen(acABCFile, "rb")) == NULL) {
                           return ( NG );
               }
                   fread(ulRecordCount,8,1,fp);
               fprint(fp_outFile, "Record Count : %lu\n", ulRecordCount);
                   for(i = 0; i< ulRecordCount; i++){
                           fread(ulDatabaseID, 8, 1, fp);
                           fread(ulAlbumID, 8, 1, fp);
                           fread(ulVolumeID, 8, 1, fp);
                           fread(ulSectionID, 8, 1, fp);
                           fread(ulLayerID, 8, 1, fp);
                           fread(ulNameCount, 8, 1, fp);
                           for(j =0; j<ulNameRecordCount; j++) {
                             fread(ucLanguageCode, 1, 1, fp);
                             fread(usNameType, 2, 1, fp);
                             fread(acString, 40, 1, fp);
                   fprint(fp_outFile, "DBID:%lu\t AlbumID:%ul\t VolID:%lu\t SecID:%lu\t LayID:%lu\t \n NameCount:%lu\n LangCode:%s\t NameType:%s\t String:%s\n", ulDatabaseID,ulAlbumID,ulVolumeID,ulDatasetID, ulSectionID,ulLayerID,usTypeCode,usSubTypeCode,usFeatureClassCode,ulNameRecordCount,aucAttributeType,ucLanguageCode,ucFont,usNameType,acString);
                               }
               }
              fclose( fp);             
             return( OK );
   }
    
 /*======================================================================
        FUNCTION     : Get XYZ File
        RETURN       : OK / NG
 ======================================================================*/
   int iGetXYZFile(char *acXYZFile)
   {
               FILE     *fp;
               int        i;
               unsigned long  ulRecordCount, ulDatabaseID, ulAlbumID, ulVolumeID;
               unsigned long  ulDatasetID, ulSectionID, ulLayerID;
               if ((fp=fopen(acXYZFile, "rb")) == NULL) {
                           return ( NG );
               }
               fread(ulRecordCount,8,1,fp);
               fprint(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++) {
                           fread(ulDatabaseID, 8, 1, fp);
                           fread(ulAlbumID, 8, 1, fp);
                           fread(ulVolumeID, 8, 1, fp);
                           fread(ulDatasetID, 8, 1, fp);
                           fread(ulSectionID, 8, 1, fp);
                           fread(ulLayerID, 8, 1, fp);
              fprint(fp_outFile, "DBID:%lu\t AlbumID:%ul\t VolID:%lu\t DatasetID:%lu\t SecID:%lu\t LayID:%lu \n", 
   ulDatabaseID,ulAlbumID,ulVolumeID,ulDatasetID, ulSectionID,ulLayerID);
               }
              fclose( fp);              return( OK );
   }

>> But there is some problem with the code can anyone help me?

Some problem? Ok whats this iGetABCFile(char*)? You can't call functions like this
You have bunch of variables wich are not declared like:
ulNameRecordCount, usSubTypeCode, usSubTypeCode, usFeatureClassCode, aucAttributeType, ucFont.
You try to fprintf them to file but not readed from binary file.

Hi,

I have modified the program, It is compiling with out any errrors, but giving a segmentation fault while running it. Can anyone help??

FILE     *fp_inFile; 
   FILE    *fp_outFile;
   static int iGetSUNFile(FILE*, FILE*);
   static int iGetSTARFile(FILE*, FILE*);
   static int iGetMOONFile(FILE*, FILE*);
   char ctype;
   char caoutfile[50]; //output file
   /******* Sample main function ********/
   int main( int argc, char **argv ) {
               /* Checking for correct number of arguments */ 
               if((argc == NULL)||(argc > 3)||(argc < 3)) {
               printf("\tErr -> command line\n");
               printf("\tUSAGE : FilesDump Type Filename\n");
                           exit(NG);
     }
     else {
               printf("\tCommand accepted  -> Type :[%s] FileName :(%s)\n",argv[1],argv[2]);
     }
     /* fopen */
               if(NULL==(fp_inFile=fopen(argv[2],"rb"))) {
               printf("IN File Open Err! -> %s\n",argv[2]);
               exit(NG);
               } 
     /* Copying the inputfile name and concatinating ".txt" to the output file */  
     strcpy(caoutfile, argv[2]);
     strcat(caoutfile,".txt");
     strcat(caoutfile, "\0");
     printf("\tNew FileName = %s\n",caoutfile);
               if ((fp_outFile=fopen(caoutfile, "w")) == NULL) {
               printf( "OutFile Open Error\n" );
               exit( NG );
               }
                  if(strcmp(argv[1],"SUN") == 0) {
                            ctype=0;
                  }
                            else if(strcmp(argv[1],"STAR") == 0) {
                              ctype=1;
                            }
                              else if(strcmp(argv[1],"MOON") == 0) {
                                        ctype=2;
                              }
               /* recognising the type of File Entered at command line */
               switch(ctype)
                 {
                 case 0 :
                           if(NULL == iGetSUNFile(fp_inFile, fp_outFile))
                             exit(NG);
                           break;
                           
                 case 1 :
                             if(NULL == iGetSTARFile(fp_inFile, fp_outFile))
                             exit(NG);
                           break;
    
                 case 2 :
                           if(NULL == iGetMOONFile(fp_inFile, fp_outFile))
                             exit(NG);
                           break;
    
                 default : printf("\tError -> Type must be one of the following – SUN, STAR, MOON \n");
                           break;
                 }
       exit( OK );
   }
   /*======================================================================
        FUNCTION     : Get SUN File
        RETURN       : OK / NG
   ======================================================================*/
   int iGetSUNFile(FILE* fp_inFile, FILE* fp_outFile)
   {
               int        i,j;
               unsigned long  ulRecordCount=0, ulDatabaseID=0, ulAlbumID=0, ulVolumeID=0;
               unsigned long  ulSectionID=0, ulLayerID=0, ulRecordID=0, ulNameRecordCount=0;
               unsigned short usNameType=0;
               unsigned char  ucLanguageCode;
               char           acString[40];
               fread((void *)ulRecordCount,8,1,fp_inFile);
               fprintf(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++){
                           fread((void *)ulDatabaseID, 8, 1, fp_inFile);
                           fread((void *)ulAlbumID, 8, 1, fp_inFile);
                           fread((void *)ulVolumeID, 8, 1, fp_inFile);
                           fread((void *)ulSectionID, 8, 1, fp_inFile);
                           fread((void *)ulLayerID, 8, 1, fp_inFile);
                           fread((void *)ulNameRecordCount, 8, 1, fp_inFile);
                           for(j =0; j<ulNameRecordCount; j++) {
                             fread((void *)ucLanguageCode, 1, 1, fp_inFile);
                             fread((void *)usNameType, 2, 1, fp_inFile);
                             fread((void *)acString, 40, 1, fp_inFile);
               fprintf(fp_outFile, "DBID:%lu\t AlbumID:%ul\t VolID:%lu\t SecID:%lu\t LayID:%lu\t \n NameRecordCount:%lu\n LangCode:%us\t NameType:%us\t String:%s\n", ulDatabaseID,ulAlbumID,ulVolumeID, ulSectionID,ulLayerID, ulNameRecordCount, ucLanguageCode, usNameType,acString);
                           }
               }
               fclose( fp_inFile);
               fclose(fp_outFile);
               return( OK );
   }
   /*======================================================================
        FUNCTION     : Get STAR File
        RETURN       : OK / NG
   ======================================================================*/
   int iGetSTARFile(FILE* fp_inFile, FILE* fp_outFile)
   {
               int        i;
               unsigned long  ulRecordCount=0, ulDatabaseID=0, ulAlbumID=0, ulVolumeID=0;
               unsigned long  ulDatasetID=0, ulSectionID=0, ulLayerID=0, ulRecordID=0;
               fread((void *)ulRecordCount,8,1,fp_inFile);
               fprintf(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++) {
                           fread((void *)ulDatabaseID, 8, 1, fp_inFile);
                           fread((void *)ulAlbumID, 8, 1, fp_inFile);
                           fread((void *)ulVolumeID, 8, 1, fp_inFile);
                           fread((void *)ulDatasetID, 8, 1, fp_inFile);
                           fread((void *)ulSectionID, 8, 1, fp_inFile);
                           fread((void *)ulLayerID, 8, 1, fp_inFile);
    
               fprintf(fp_outFile, "DBID:%lu\t AlbumID:%ul\t VolID:%lu\t DatasetID:%lu\t SecID:%lu\t LayID:%lu\t \n", ulDatabaseID,ulAlbumID,ulVolumeID,ulDatasetID, ulSectionID,ulLayerID);
               }
               fclose( fp_inFile);
               fclose(fp_outFile);
               return( OK );
   }
   /*======================================================================
        FUNCTION     : Get MOON File
        RETURN       : OK / NG
   ======================================================================*/
   int iGetMOONFile(FILE* fp_inFile, FILE* fp_outFile)
   {
               int        i;
               unsigned long  ulRecordCount=0, ulDatabaseID=0, ulAlbumID=0, ulVolumeID=0;
               unsigned long  ulDatasetID=0, ulSectionID=0, ulLayerID=0, ulRecordID=0;
               long    lLatitude=0, lLongitude=0, lAltitude=0;
               fread((void *)ulRecordCount, 8,1,fp_inFile);
               fprintf(fp_outFile, "Record Count : %lu\n", ulRecordCount);
               for(i = 0; i< ulRecordCount; i++) {
                           fread((void *)ulDatabaseID, 8, 1, fp_inFile);
                           fread((void *)ulAlbumID, 8, 1, fp_inFile);
                           fread((void *)ulVolumeID, 8, 1, fp_inFile);
                           fread((void *)ulDatasetID, 8, 1, fp_inFile);
                           fread((void *)ulSectionID, 8, 1, fp_inFile);
                           fread((void *)ulLayerID, 8, 1, fp_inFile);
                           fread((void *)lLatitude, 8, 1, fp_inFile);
                           fread((void *)lLongitude, 8, 1, fp_inFile);
                           fread((void *)lAltitude, 8, 1, fp_inFile);
    
               fprintf(fp_outFile, "DBID:%lu\t AlbumID:%lu\t VolID:%lu\t DatasetID:%lu\t SecID:%lu\t LayID:%lu\t \n Lat:%d\t Long:%d\t Alt:%d\n", ulDatabaseID,ulAlbumID,ulVolumeID,ulDatasetID, ulSectionID,ulLayerID,lLatitude,lLongitude,lAltitude);
               }
               fclose( fp_inFile);
               fclose(fp_outFile);
               
               return( OK );
   }

This is wrong
fread((void *)ucLanguageCode, 1, 1, fp_inFile);
instead you should correct to
fread(&ucLanguageCode, 1, 1, fp_inFile);
So all (void *) in fread correct to &

Thankx a Lot!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.