C Program to Create a File and Write Data into It

In this article, we will write a C program to create a file and write data into it. The program takes the name of the file that is to be created as an input from the user, asks the user to enter some content, and writes it into the newly created file.

Sample Input:

Enter the name of the file: myfile.txt
Enter content to write into the file:
This is some random content to write into the file

Sample Output:

File created successfully!

In C programming language, you can very easily create a new file by simply calling the fopen() function in write mode(“w”) or binary write mode(“wb”).

The fopen() is basically used to open a file. It needs two parameters to do this, first the name or path of the file and second the mode in which the file should be opened.

But if you open up the file in write mode(“w”) and the file name or file path you have specified does not exist, in that case, the fopen() function creates a new file with the specified file name.

Now, if you want to write content into this file, you can call the fputc() or fputs() functions.

See implementation in the following program:

// C program to create a new file
// and write data into it

#include <stdio.h>

int main(){
	
	char filename[100], data[1000];
	FILE *fptr;
	
	printf("Enter the name of the file: ");
	scanf("%s", &filename);
	
	// Open and create new file
	fptr = fopen(filename, "w");  
	
	if(fptr == NULL){
		printf("Unable to create new file");
		return 1;
	}
	
	// To clear input stream(It's optional)
	// Use this if program is not waiting to take user input
	fflush(stdin);  
	
	printf("Enter content to write into the file: \n");
	fgets(data, 1000, stdin);
	
	// Write data into the file
	fputs(data, fptr);
	
	printf("File created successfully!");
	
	// Close the file
	fclose(fptr);
	
    return 0;
    
}

Output:

Enter the name of the file: myfile.txt
Enter content to write into the file:
This is some random content to write into the file
File created successfully!

Below is a sample output of file creation using the above program:

Output of - C Program to Create a File and Write Data into It

Program Explanation:

Below is the step-by-step explanation of the above program:

  • The program starts by prompting the user to enter the name of the file they want to create. The file name entered by the user is stored in the filename character array.
  • The program declares a file pointer fptr of type FILE that will be used to work with the newly created file.
  • The program attempts to open or create a new file with the specified name in write mode ("w") using the fopen() function. If the file already exists, opening it in write mode will clear its previous content. If it doesn’t exist, a new empty file with the specified name will be created.
  • The program checks if the file pointer fptr is NULL. If it is, it means there was an issue with creating or opening the file and an error message is displayed:
  • The program prompts the user to enter the content they want to write into the file. The user can input multiple lines of text, and the entered content is stored in the data character array with a maximum size of 1000 characters using fgets().
  • The program uses the fputs() function to write the content stored in the data array into the newly created file.
  • After successfully writing the content into the file, the program prints a success message. The program closes the file using fclose(fptr) to release system resources.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.