title: How to read all contents of file, including NUL chars between valid text?
body: <p>I have a file that I need to read into a buffer (char *) but the problem is that the file has some "funny characters" in it inbetween the valid text.</p>
  
  <p><img src="https://i.stack.imgur.com/J5Od1.png" alt="enter image description here"></p>
  
  <p>So when I write some code like the following:</p>
  
  <pre><code>  FILE *fp;
    if((fp = fopen(".\\test.txt", "rt"))==NULL){
      printf("Cannot open file2\n");
    }
  
    fseek(fp, 0, SEEK_END);
    long int fsize = ftell(fp);
    rewind(fp);
    //char *buffer2 = malloc(fsize * sizeof(char));
    buffer = malloc(fsize * sizeof(char));
    fread(buffer, 1, fsize, fp);
    buffer[fsize] = '\0';
    fclose(fp); 
  
    printf("fsize = %i\n", fsize);
    printf("Buffer = %s\n", buffer);
  </code></pre>
  
  <p>It only prints out the first part of the text file like follows:</p>
  
  <p>Buffer = header</p>
  
  <p>And obviously stops at the first NUL char.</p>
  
  <p>Is thier any way to read the entire buffer of the file, including funny chars?</p>
  
  <p>Or is this not possible in C?</p>
  
  <p>The FSIZE is read correctly, just the FREAD does not read the entire buffer ;-(</p>
  
  <p>Any help would be greatly appreciated ;-)</p>
  
  <p>Thanks</p>
  
  <p>Lynton</p>
  
  <p>UPDATE: OK, I was being a little stupid.....if I write the buffer to a file it has everything in it, only if I write it out to the screen does it stop at the null so that is fine.</p>