By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

convert binary file to hexadecimal... Please help

P: 11
Hi,
I have to convert to a hexadecimal file. I'am new to file handling so kindly review my code and tell me why does it crash!
Expand|Select|Wrap|Line Numbers
  1. //As inputs, I use:
  2.  // hexdump_c.cpp
  3. // gif.txt  /* after this, the error report screen from Microsoft comes up, saying the program has to close */
  4. // Press any key to continue . . 
  5. #include <stdio.h>
  6.  
  7. int main(int argc,char* argv[])
  8. {
  9.     FILE* fileHandle;
  10.  
  11.     scanf("%s",argv[0]);
  12.     scanf("%s",argv[1]);
  13.     printf("\n%d",argc);
  14.     printf("\n%s",argv[0]);
  15.     printf("\n%s\n",argv[1]);
  16.     if (argc != 2)
  17.     {
  18.         printf("Usage: hexdump <file name>\n");
  19.         return -1;
  20.     }
  21.  
  22.     fileHandle = fopen(argv[1],"rb");
  23.     if (!fileHandle)
  24.     {
  25.         printf("Error: failed to open file %s for read",argv[1]);
  26.         return -1;
  27.     }
  28.     /* dump data here */
  29.  
  30.  
  31.     fclose(fileHandle);
  32.     return 0;
  33. }
  34. unsigned char lower(unsigned char c)
  35. {
  36.     unsigned char one_byte;
  37.     one_byte = c & 0x0F;
  38.     return one_byte;
  39. }
  40. unsigned char upper(unsigned char c)
  41. {
  42.     unsigned char one_byte;
  43.     one_byte = (c & 0xF0) >> 4;
  44.     return one_byte;
  45. }
  46.  
Oct 2 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
scanf("%s",argv[0]);
scanf("%s",argv[1]);

you absolutely should not write to argv, it contains the command line parameters, if there are any. The is always 1, the program name, but it doesn't necessarily point to any more data than is required to hold the parameters.
Oct 2 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.