-- Comments about reading the text file --
-1- The size is unknown: it may less or more than SOURCE_BUFFER_SIZE (10^5)
-2- fread() does not produce the end-of-string delimiter (null byte) that you check for end of read loop
On my side, here is what I use:
#include <io.h>
/*------------------------------------------------------------------------------*/
static char *afileLoad(char *fname) /* loads a file in allocated text */
/*--------------------------------------------------------------------------------
This function returns a pointer on the text buffer allocated for the file content.
This pointer can be used for further freeing. It is NULL in case of error.
--------------------------------------------------------------------------------*/
{
char *buffer = NULL ;
int h = _open(fname, 0) ;
if (h)
{
long size = _filelength(h) ;
if (size > 0)
{
buffer = calloc(size + 1, 1) ; /* size + end null character */
if (buffer) _read(h, buffer, size) ;
}
_close(h) ;
}
return buffer ;
}
Note that calloc initialize buffer with null bytes. That way I can safely loop like this:
char c, *cp ;
for (cp = buffer ; c = *cp ; cp++)
{
// c is the current character
// cp points on the next one
}
One gets a warning on the test for end-of-loop:
warning #2030: = used in a conditional expression.
It is not a mistake.
I hope this comments usefull.