Thread: how do I increment a file pointer by eight bytes exactly?

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    27

    how do I increment a file pointer by eight bytes exactly?

    I am trying to write a program that will read eight bytes from a file, perform some math on it and then read in eight more bytes untill there is nothing left to read even if the last group is < 8 bytes. Do I need an array?
    Thanks
    Last edited by Once-ler2; 01-14-2013 at 12:55 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Lookup the fread() function, it does exactly what you need.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look up fread()

    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

    If you pass 8,1 as the middle 2 parameters, you'll either read 8 bytes or nothing, and the result will be either 1 or 0.

    If you pass 1,8 as the middle 2 parameters, you'll read 8 bytes at most, but you might read anything from 0 to 7 as you get to the end of the file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Using an array is one option. It is not the only option, nor is it mandatory. That all depends on what the "math" entails, and anything else your code is trying to achieve.

    Your thread title has nothing to do with the question either. Reading data from a file and incrementing a file pointer are not necessarily related concepts.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does fflush increment file pointer??
    By juice in forum C Programming
    Replies: 9
    Last Post: 12-28-2011, 09:32 PM
  2. Got Error in Pointer increment. Help !!!
    By mysql2779 in forum C Programming
    Replies: 9
    Last Post: 03-19-2011, 02:10 AM
  3. Increment a pointer variable
    By skiabox in forum C Programming
    Replies: 5
    Last Post: 11-17-2010, 11:24 AM
  4. pointer increment error
    By WDT in forum C++ Programming
    Replies: 7
    Last Post: 12-09-2007, 08:01 AM
  5. Increment the address stored in a pointer???
    By patricio2626 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2007, 06:36 PM