I am using the win32 CreateFile and ReadFile API to read Files on my HD but it stops reading when it encounters a null termination('/0').

Could you tell me of a way to overcome this and continue to read the whole file.

please post a sample of the code where you use ReadFile(). Your problem may come from a bad use of the function.

anyway, a more portable way to read files in C is to use the functions from the standard library when you do not have special things to do.

#include <stdio.h>
#include <malloc.h>
/** returns a newly allocated  array that holds the whole content of a
   * file. the int value passed as argument is filled with the length of 
   * the file.
   */
char * get_whole_file(const char* filename, int *size)
{
     FILE* f;
     char* buffer;
     *size=0;
     int read;
     f=fopen(filename, "rb");
     if(f==NULL)return 0;
     fseek(f, SEEK_END);
     *size=ftell(f);
     fseek(f, SEEK_SET);
     buffer=(char*)malloc(sizeof(char)*(*size));
     read=fread(buffer, *size, sizeof(char), f);
     fclose(f);
     if(read != *size * sizeof(char))
     {
            free(buffer);
            return NULL;
     }
     return buffer;   
}

please post a sample of the code where you use ReadFile(). Your problem may come from a bad use of the function.

anyway, a more portable way to read files in C is to use the functions from the standard library when you do not have special things to do.

#include <stdio.h>
#include <malloc.h>
/** returns a newly allocated  array that holds the whole content of a
   * file. the int value passed as argument is filled with the length of 
   * the file.
   */
char * get_whole_file(const char* filename, int *size)
{
     FILE* f;
     char* buffer;
     *size=0;
     int read;
     f=fopen(filename, "rb");
     if(f==NULL)return 0;
     fseek(f, SEEK_END);
     *size=ftell(f);
     fseek(f, SEEK_SET);
     buffer=(char*)malloc(sizeof(char)*(*size));
     read=fread(buffer, *size, sizeof(char), f);
     fclose(f);
     if(read != *size * sizeof(char))
     {
            free(buffer);
            return NULL;
     }
     return buffer;   
}

Here is my code:

#include <windows.h>
#include <stdio.h>
/*
//THIS CAN BE COMPILED IN C
int main()
{
  HANDLE hFileR;   //hFile is for Create & write, hFileR for Reading
  char buffer[200];
  memset(buffer, '\0', sizeof(buffer));

  DWORD dwTest = sizeof(buffer);
  hFileR = CreateFile("c:\\ty", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  ReadFile(hFileR, buffer, dwTest, &dwTest, NULL);
  puts(buffer);
  char master[200];
  snprintf(buffer, sizeof(buffer), "/0", master);
  puts(master);
  return 0;
}  */

in fact your code actually reads the 200 bytes that you request, but it wont display it entirely as a string if there is \0 characters in the middle.

You could choose to transform \0 into spaces :

#include <windows.h>
#include <stdio.h>

//THIS CAN BE COMPILED IN C
int main()
{
  HANDLE hFileR;   //hFile is for Create & write, hFileR for Reading
  char buffer[200];
  memset(buffer, '\0', sizeof(buffer));
  DWORD dwTest = sizeof(buffer);
  printf("sizeof(buffer)=%d\n",dwTest);
  hFileR = CreateFile("c:\\ty", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  ReadFile(hFileR, buffer, dwTest, &dwTest, NULL);
  
  {
      int i;
      for(i=0; i<200-1;i++)if(buffer[i]=='\0')buffer[i]=' ';/*we transform \0 into spaces*/
      buffer[200-1]='\0';/*last byte must be null, anyway*/
  }
  
  puts(buffer);
  char master[200];
  snprintf(buffer, sizeof(buffer), "/0", master);
  puts(master);
  system("pause");
  return 0;
}

I don't understand what you try to do with snprintf afterwards. The signature for snprintf is

int snprintf(char *str, size_t size, const char *format, ...);

with str= the destination stirng
size = the size of the destination string
format = what to copy into destination (same syntax as printf)

correction : about get_whole_file() :

As usual i posted code that do not compile ! (all my apologies, I wont do that again ;) )

replace fseek(f, SEEK_END) by fseek(f, 0, SEEK_END)
replace fseek(f, SEEK_SET) by fseek(f, 0, SEEK_SET)
replace read=fread(buffer, *size, sizeof(char), f); by read=fread(buffer, sizeof(char), *size, f);
:o

This article has been dead for over six months. Start a new discussion instead.