Results 1 to 5 of 5

Thread: Bytes

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Far.. far away! (Netherlands)
    Posts
    169

    Bytes

    Hello everyone!

    I got a question, hope someone knows a answer
    Im using fopen and fgetc to read bytes from a file. How do i read a integer from a file this way? I want to read a header of a file.

    Thanks in advance,

    -Shell-

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Just read 4 bytes, and there's your int (32-bit, of course...)
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Far.. far away! (Netherlands)
    Posts
    169
    Ah, so its possible. Can you give me an example on how to do this? How to stuff those bytes into one integer i mean, i now.. i'm a newbie

    -Shell-

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    int i;
    fread(&i, sizeof(int), 1, fp);
    ...I think, been a while since I did C

    fread(target, size, count, file), where size is the size of each element, and count is how many you're reading. In this case, you're reading 1 4-byte number.



    I assume it's a binary file, right...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2002
    Location
    Far.. far away! (Netherlands)
    Posts
    169
    Thanks! That works!

    -Shell-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width