Thread: Reading hex

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    11

    Reading hex

    Okay, I've got a small problem going on. Here goes.
    Code:
    struct CD {
      unsigned int ID;
      char *Title;
      char *Artist;
      char *Genre;
      int  Year;
      int  Time;
      int  Number_Tracks;
      struct Songs *Track_Ptr;
    };
    
    void dbload()
    {
      char dbname[MAXFILELEN];
      FILE *fp1;
      int cdnum;
      int i;
      struct CD CDray, *ptr;
      
      ptr = &CDray;
      
      printf("Enter file to load: ");
      scanf("%s", &dbname);
      
      if ((fp1 = fopen(dbname, "r")) == NULL)
      {  
        printf("Cannot open file\n");
        return;
      }
    
      fscanf(fp1, "%x\t", ptr->ID);
      printf("%d",&ptr->ID);
                        
      printf("Load successful\n");
    }
    ID inside the file should be a hex number, for example: 820c100a
    I know %x should read it in, but it crashes each time at that point. Also, when I print it, I want to print it as a hex number, which should again be %x unless I'm mistaken.
    Any ideas why I'm crashing?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    fscanf(fp1, "%x\t", ptr->ID);
    warning: format argument is not a pointer (arg 3)
    And there are better ways to obtain a string than this:
    Code:
      scanf("%s", &dbname);
    See the FAQ.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    I'm not concerned about the string grab, as it is working.
    I'm not sure what you mean with the pointer...

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by jrfall349 View Post
    I'm not concerned about the string grab, as it is working.
    A "works for me" attitude is not a good one to have. Using scanf() with that format string is dangerous and can lead to your program being taken over by a malicious user.

    Quote Originally Posted by jrfall349 View Post
    I'm not sure what you mean with the pointer...
    ptr->ID is an int. You need to be passing the address of an int to scanf() in this case or else it will write to the wrong location in memory.

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    11
    I can't believe I forgot that frackin '&' again! Thanks. And I meant I wasn't concerned about the string for now.... But thanks for the heads up on it anyway.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    To read a string safely, try the following:

    Declare a new int variable local to your function. Let's call it iEnd.

    Replace this line:

    Code:
    scanf("%s", &dbname);
    With this:

    Code:
    fgets(dbname,sizeof(dbname),stdin);
    iEnd = strlen(dbName) - 1;
    if(dbname[iEnd] == '\n')
    {
    	dbname[iEnd] = '\0';
    }
    Your program should be safe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. Fun with reading hex
    By dpro in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2006, 06:41 PM
  3. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  4. write to hex, is it possible?
    By keithmolo in forum C++ Programming
    Replies: 12
    Last Post: 07-30-2003, 02:27 PM