Problem with fread() and Hex

This is a discussion on Problem with fread() and Hex within the C Programming forums, part of the General Programming Boards category; Hi guys, this is my first post! I like programming C and I like to find here a lot of ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Unhappy Problem with fread() and Hex

    Hi guys, this is my first post!
    I like programming C and I like to find here a lot of friends!

    I've done a program that read a midi file and from its hex code, program obtains feature (tracks,formats,instrument...).

    Now I've got a problem with fread().

    Code:
    #include <stdio.h>
    main()
    {
    int n,i;
    FILE *fp;
    short int data[10];
    fp = fopen("formato.mid","r");
    if (fp!=NULL)
    {
    fread(data,2,10,fp);
    for (i=0;i<10;i++)
    {
    printf("Print fread buf: %x\n",data[i]);
    }
    }
    fclose(fp);
    }
    If hex code is 4D54, my program set (so read) in array: 544D and so print: "Print fread: 544D". I need my program reads correct code and not contrary.

    Do you know how could I solve it and why happens?

    Thanks!!!

  2. #2
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    I guess it's about endianess problem.

    an array of char (or unsigned char) probably could solve your problem.

  3. #3
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    @zulsachiyoza : if the deadline is next week, you could read the C tutorial in 24 hours and then you can do your own homework
    Last edited by cph; 09-22-2010 at 06:37 AM.

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    I've tried, but it save 4D in an array's position and 54 in another array's position so it can read only from -255 to 255.

    And I've inserted dimension 2 in fread().

    I need 4D54 in a single array position.

    Zulsachiyoza....please...

  5. #5
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    ok, just swap them

    and I suggest you do this
    Code:
      fread(data, sizeof(*data), sizeof(data) / sizeof(*data), fp);
    instead of
    Code:
      fread(data, 2, 10, fp);
    Last edited by cph; 09-22-2010 at 06:50 AM.

  6. #6
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    How? I don' understand.

    I need a single number of 2 dimension (2 byte: 4d54=19796), in a single array's position...Now I've got 2 numbers divided in 2 position, with char array.
    4D=77
    54= 84

    If I use short int array I've got 544d =21581 and it is wrong...

  7. #7
    cph
    cph is offline
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    82
    you could use a union
    Code:
    typedef union {
      short a;
      char  b[2];
    } short_u;
    and then, swap the b[0] and b[1].

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stefanostefano View Post
    Hi guys, this is my first post!
    I like programming C and I like to find here a lot of friends!

    I've done a program that read a midi file and from its hex code, program obtains feature (tracks,formats,instrument...).
    I've done some midi programming in the past and I'm wondering how well you understand the midi file format...

    First off Midi files are not flat files, they are compressed files with variable sized elements. It's not like reading a text or hex file, at all. Decoding these event elements is not a simple matter. One needs to read the entire file into a memory buffer (thank goodness they're small) and burst it out into an array mdi events. These files actually use 7bit data words and to compress they flow them together, so you're going to find bits of one value in 2 different bytes.

    I suggest you follow the link to see exactly what you are getting yourself into...
    MIDI Technical Fanatic's Brainwashing Center

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fread problem
    By win in forum C Programming
    Replies: 4
    Last Post: 11-14-2009, 11:19 AM
  2. Displaying a whole file in hex
    By samus250 in forum C Programming
    Replies: 4
    Last Post: 06-30-2008, 03:31 PM
  3. Replies: 1
    Last Post: 04-05-2002, 10:19 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21