May 10, 2024

Write Numerical or Text Data to File in C Programming Language

In this post, we explain how to save numerical or text data to a *.txt file in the C programming language. The YouTube video accompanying this post is given below.

To work with files in the C programming language, we need to declare a pointer to the FILE type:

FILE *file;

Here, “file” is a pointer to the “FILE” type. Then, we can open the file by using the “fopen()” function:

FILE *fopen(const char *filename, const char *mode)

where

  • “filename” is the C string that denotes the name of the file to be opened
  • “mode” is the C string denoting the mode of opening

Here are several commonly used modes:

  • “r” – opens a file for reading. The file must exist before opening.
  • “w” – creates an empty file for writing the data. If the file does not exist it is created. If the file exists, then the content of the file is overwritten. That is, if the file exists, it content is erased, and new data is written from the beginning of the file.
  • “a” – appends new data to a file, starting from the end of the file. If the file does not exist, then it is created, and data is written.

There are also other modes, however, for the brevity of this introductory tutorial, we do not explain them. The function “fopen”, returns a FILE pointer that can be used in other functions. If the file cannot be opened, NULL is returned. We can write the data to the opened file by using the “fprintf()” function. Once we have completed the writing task, we need to close the file by using the “fclose()” function.

The following code explains how to open a file, write two data columns to the file, and close the file.

#include <stdio.h>
// this file explains how to save data to a file in C
int main()
{
	//file name that is used to store the data
	// this is a string literal
	char const *file_name="data_file.txt";
	int j=0; // not allowed in the for loop to define a counter 
	// if you declare j in the loop, you will get this error: [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode
	
	// open the file for writting, "w" option will delete the content of the file
	FILE *file=fopen(file_name,"w");
	
	if (file==NULL)
	{
		printf("Could not open the file %s",file_name);
		return 0;
	}
	
	for (j=0; j<10; j++)
	{
		fprintf(file,"%d,%f\n", j, (float)1.2*j);
	}
	
	
	fclose(file);
	
	return 1;
}

The previous implementation uses the function “fprintf()” which can be used to add a complete text line to the file. We can also write a single character to the file. This can be achieved by using “fputc()”. The following code lines explain this approach.

#include <stdio.h>
// this file explains how to save data to a file in C, character by character
int main()
{
	//file name that is used to store the data
	// this is a string literal
	char const *file_name="data_file2.txt";
	int j=0; // not allowed in the for loop to define a counter 
	char character;
	// if you declare j in the loop, you will get this error: [Error] 'for' loop initial declarations are only allowed in C99 or C11 mode
	
	// open the file for writting, "w" option will delete the content of the file
	FILE *file=fopen(file_name,"w");
	
	if (file==NULL)
	{
		printf("Could not open the file %s",file_name);
		return 0;
	}
	
	for (character='a'; character<='z' ; character++)
	{
		fputc(character,file);
	}
	
	fclose(file);
	
	return 1;
}