help with this code!!

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
30
31
32
#include <stdio.h>
#include <stdlib.h>

int main () {
  FILE * pFile;
  long lSize;
  char * buffer;
  size_t result;

  pFile = fopen ( "myfile.bin" , "rb" );
  if (pFile==NULL) {fputs ("File error",stderr); exit (1);}

  // obtain file size:
  fseek (pFile , 0 , SEEK_END);
  lSize = ftell (pFile);
  rewind (pFile);

  // allocate memory to contain the whole file:
  buffer = (char*) malloc (sizeof(char)*lSize);
  if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);}

  // copy the file into the buffer:
  result = fread (buffer,1,lSize,pFile);
  if (result != lSize) {fputs ("Reading error",stderr); exit (3);}

  /* the whole file is now loaded in the memory buffer. */

  // terminate
  fclose (pFile);
  free (buffer);
  return 0;
}


how to open binary for read and write?
why the buffer is char * buffer? i mean in binary u cant read chars .
how can it be? how the data is represented? just like txt file? HELP!!
what the buffer will contain how to print this buffer???
HELP
Last edited on
BUMP
A char array is just a convenient way of creating an array of bytes, because the size of a char is 1 byte. So each char contains 1 byte of the binary data.

And really, 43 minutes is no time at all to wait for a reply. You're being a bit trigger-happy on the bumping.
answer my other questions. how i can print the buffer content how the data is represented? in ascii code like txt files? how????
Topic archived. No new replies allowed.