Writing Memory to a File and Reading Memory from a File in C

In this C programming language tutorial we will look at how to save content from an allocated piece of memory to a (binary) file. We also read it back from a (binary) file into memory and display the content.

Because the program example is longer than the usual examples that we use in our tutorials, we’ve split it into parts. Further down you’ll find an explanation for each part of the source.

Please note: that in a normal situations we would have placed some parts in a functions. But for teaching purposes we have created one large program.

Without further delay, here is the source code of the example:


	#include <stdio.h>
	#include <stdlib.h>

	int main(void)
	{
		int counter;
		double *ptr_d;
		FILE *ptr_fp;

		/* Part A */
		ptr_d = (double *)malloc(10 * sizeof(double));
		if(!ptr_d)
		{
			printf("Memory allocation error!\n");
			exit(1);
		}else printf("Memory allocation successful.\n");

		/* Part B */
		for(counter = 0; counter < 10; counter++)
			ptr_d[counter] = (double) rand();

		/* Part C */
		if((ptr_fp = fopen("test.txt", "wb")) == NULL)
		{
			printf("Unable to open file!\n");
			exit(1);
		}else printf("Opened file successfully for writing.\n");

		/* Part D */
		if( fwrite(ptr_d, 10*sizeof(double), 1, ptr_fp) != 1)
		{
			printf("Write error!\n");
			exit(1);
		}else printf("Write was successful.\n");
		fclose(ptr_fp);
		free(ptr_d);

		/* Part E */
		ptr_d = (double *)malloc(10 * sizeof(double));
		if(!ptr_d)
		{
			printf("Memory allocation error!\n");
			exit(1);
		}else printf("Memory allocation successful.\n");

		/* Part F */
		if((ptr_fp = fopen("test.txt", "rb"))==NULL)
		{
			printf("Unable to open the file!\n");
			exit(1);
		}else printf("Opened file successfully for reading.\n");

		/* Part G */
		if(fread(ptr_d, 10 * sizeof( double ), 1, ptr_fp) != 1)
		{
			printf( "Read error!\n" );
			exit( 1 );
		}else printf( "Read was successful.\n" );
		fclose(ptr_fp);

		/* Part H */
		printf("The numbers read from file are:\n");
		for(counter = 0; counter < 10; counter++)
			printf("%f ", ptr_d[counter]);

		/* Part I */
		free(ptr_d);
		return 0;
	}

Part A: Memory Allocation

After some variable declaration we start the program with a memory allocation statement. A piece of memory is requested using malloc with a size of 10 times the size of a double. A pointer ptr_d is used to point to this piece of memory. The rest of part A does some simple error handling.

Part B: Generating Random Numbers

Part B is used to generate some random numbers (using rand function) that are placed into our allocated piece of memory. We request for 10 random numbers. The rest of part B does some simple error handling.

Part C: Open a File for Writing

Part C creates an empty file for writing. If a file with the same name already exists its content is erased and the file is treated as a new empty file. In order to open a file as a binary file (instead of text), a “b” character has to be included in the mode string. The rest of part C does some simple error handling.

Part D: Write the Array

In part D the content of the array is written to the file that was opened in part C. After the array content has been written to the file, the file is closed (fclose function) and the allocated memory is released (free function.) The rest of part D does some simple error handling.

Part E: Memory Allocation

Once again a piece of memory is allocated. This piece of memory is used to write the memory to after we have read it from the file in the next part. The rest is some simple error handling.

Part F: Open Binary File for Reading

Almost the same as part C, but instead of opening the file for writing the file is opened for reading by using “rb” in the mode string. The rest is some simple error handling.

Part G: Reading the Binary File

Almost the same as writing the entire array to the file, but instead we use the fread function to read the content of the file. After the read the file is closed using the function fclose. The rest is some simple error handling.

We now have written memory content to a file and read the content of the file back into another piece of memory. So we could have stopped here, but it’s always nice to visualize things and that is where the last part is for.

Part H: Display Content

Once again we make a loop. This loop is used to print each member of the array onto the screen, nothing special.

Part I: Free Memory

As a last act we free the memory that we asked for in part E, so we don’t create any memory leaks.

That’s all for this tutorial. We hope that you can see the uses of the example written above and use it in your own program. The only thing I would change is to make a function for memory allocation and open/read/write function, but I leave that up to you.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 3 responses to “Writing Memory to a File and Reading Memory from a File in C”

Why not let us know what you think by adding your own comment!

  1. Ashwani on July 25th, 2011:

    very good…

  2. sandeep kosta on June 10th, 2012:

    superb work man………..god bless u……….

  3. amit on January 8th, 2013:

    superb work…!!!!