Hi,everybody.Following is an simple C programm which want to display the content of a file with corresponding hex.If one letter is a undisplayed then replace it with '.' .

info and code:

info:
[root@localhost guai]# gcc disp.c -o disp.exe
disp.c:29:9: empty character constant
disp.c: In function `main':
disp.c:47: wrong type argument to increment

#include <stdio.h>
int main(int argc,char **argv)
{
 char letter[17];
 int c,i,cnt;
 FILE *fp;
 if(argc!=2)
 {
  printf("\7USAGE:disp filename");
  exit();
 }

 if((fp=fopen(argv[1],"r"))==NULL)
 {
  printf("\7file %s can't opened\n",argv[1]);
  exit();
 }

 cnt=0;

 do
 {
  i=0;
  printf("%06x:",cnt*16);

  while((c=fgetc(fp))!=EOF)
  {
   printf("%02x",c);
   if(c<''||c>0x7e)
   {
    letter[i]='.';
   }
   else
   {
    letter[i]=c;
   }
   if(++i==16)
    break;
  }

  letter[i]='\0';
  if(i!=16)

  for(;i<16;i++)
  {
   printf(" ");
   printf("%s\n",letter++);
   cnt++;
  }
 }while(c!=EOF);

 fclose(fp);
}
Member Avatar for iamthwee

Should be fairly straightforward.

Perhaps you could read in the file character by character, then convert it to hex as you go along?

>>disp.c:29:9: empty character constant

>> if(c<''||c>0x7e)

there is no character between the single quotes. you probably meant to put a space there??

Another bug:
>>}while(c!=EOF);

the above is not needed because its not a do-while loop.

say thanks to all.let me aquire so many benefits.

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.