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
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.
Lookup the fread() function, it does exactly what you need.
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.
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.