file IO - reading an entire file into memory


return to main index


#include <stdio.h>


Often the exact structure of a document is NOT know at the time it is read. Therefore, a function such as fscanf() cannot be used because the sequence, and type, of the data must be know beforehand!

Reading text one line at a time can also be tricky unless the maximum number of characters on a line is also known beforehand!

Often the only way to deal with text, especially when it contains numeric values in unknown locations is to read it in a single block, as shown here, or read it in small chunks and use a technique called "parsing". This reference shows how an entire text file can be read into memory. Parsing is dealt with in another web page.



/* declare a file pointer */
FILE    *infile;
char    *buffer;
long    numbytes;
 
/* open an existing file for reading */
infile = fopen("test.rib", "r");
 
/* quit if the file does not exist */
if(infile == NULL)
    return 1;
 
/* Get the number of bytes */
fseek(infile, 0L, SEEK_END);
numbytes = ftell(infile);
 
/* reset the file position indicator to 
the beginning of the file */
fseek(infile, 0L, SEEK_SET);	
 
/* grab sufficient memory for the 
buffer to hold the text */
buffer = (char*)calloc(numbytes, sizeof(char));	
 
/* memory error */
if(buffer == NULL)
    return 1;
 
/* copy all the text into the buffer */
fread(buffer, sizeof(char), numbytes, infile);
fclose(infile);
 
/* confirm we have read the file by
outputing it to the console */
printf("The file called test.dat contains this text\n\n%s", buffer);
 
/* free the memory we used for the buffer */
free(buffer);

Once the buffer has been filled with a copy of the text from the source file it can used for operations such data extraction, searching/replacing and outputing to another file. More about that later!

For a full listing of this code refer to 4_IO_readall.c