how do I tell if there is byres <4

Pages: 12
Any file, the app will be blind to what to wether it is a binary or text. It is to load four bytes and edit them in hex untill Total#of bytes in the file remaining is <4 in the file.
I just need to learn how to do this for knowledge's sake.
If someone could give me some examples in C (I know this board is C++ but I just don't understand that OOP stuff), I know that some people here must have started with C.
I just need to know how to manipulate the data on a Hex scale or binary if not hex.
I need to learn how to position in my file so that when the first four bytes have been processed it loads the next four and so on.
As to the format of the file it would be the same as the input just different code in it.
Thank you.
Last edited on
In that case, always open the file in binary.

Just read four bytes at a time.

"Hex(adecimal)" is just a way of representing a number textually (so that humans can read it). It is the same in the file no matter what.

Here is an (untested) version of the binary file I/O stuff in C:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <stdio.h>

void write_word_le( FILE* outs, unsigned long value, unsigned size )
  {
  for (; size; --size, value >>= 8)
    fputc( value & 0xFF, outs );
  }

unsigned long read_word_le( FILE* ins, unsigned size )
  {
  unsigned long value = 0;
  for (unsigned n = 0; n < size; ++n)
    value |= fgetc( ins ) << (8 * n);
  return value;
  }

void write_word_be( FILE* outs, unsigned long value, unsigned size )
  {
  while (size)
    fputc( (value >> (8 * --size)) & 0xFF, outs );
  }

unsigned long read_word_be( FILE* ins, unsigned size )
  {
  unsigned long value = 0;
  for (; size; --size)
    value = (value << 8) | fgetc( ins );
  return value;
  }

This code does not account for premature EOF -- that needs to be dealt with elsewhere (an optimization). Use fseek() and ftell() to determine the size of the file.

Use the %x or %X format specifier to display the number in hex.
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

Good luck!
Thank you
Topic archived. No new replies allowed.
Pages: 12