/**
* @file 18.c
* @author jill
* @brief
* This file is used for printting the file in the form of hex as "UltraEdit".
*/
#include "stdlib.h"
#include "stdio.h"
#include "math.h"
/**
* @brief
*The function named judge_paramnum is used for judging the number of parameter.
*If the number is less than 2,it will give an information and exit the programme.
* @param argc the number of parameter.
*/
void judge_paramnum(int argc)
{
switch(argc < 2)
{
case 1:printf("Input Error!");exit(0);
}
}
/**
* @brief
* This function named get_fdata is used for getting the file name and process the data.
*If the file name is wrong,it will show an information prompts error.
*@param argv the point of the second of command line param inputed.
*/
void get_fdata(char *argv)
{
FILE *fp;
char cbuff[16],cdata;
int i,j;
int nlinenum = 0;
if((fp = fopen(argv,"rb")) == NULL)
{
printf("Can't open the file named %s.",argv);
exit(0);
}
while(! feof(fp))
{
printf("%07x0h: ",nlinenum);
for(i = 0;i < 16;i++)
{
if(feof(fp))
{
for(j = i - 1;j < 16;j++)
{
printf("%2c ",' ');
cbuff[j] = 0;
}
break;
}
else
{
fread(&cdata,sizeof(char),1,fp);
if((cdata == '\n') && (! feof(fp)))
{
cbuff[i] = '\r';
cbuff[i + 1] = '\n';
printf("%02X ",cdata & 0xff);
}
else if(! feof(fp))
{
cbuff[i] = cdata;
printf("%02X ",cdata & 0xff);
}
}
}
printf(";");
for(i = 0;i < 16;i++)
{
if((cbuff[i] == '\n') || (cbuff[i] == '\r'))
{
printf(".");
}
else
{
printf("%c",cbuff[i]);
}
}
nlinenum++;
printf("\n");
}
fclose(fp);
}
int main(int argc,char * argv[])
{
judge_paramnum(argc);
get_fdata(argv[1]);
}