A program to create a file and enter data from the user in C

In this article, you will be going to learn about create a file and enter data into that file with the help of a user in C programming.

Below we presenting, A program to create a file and enter data from the user in C

#include <stdio.h>
#include <stdlib.h>
  
int main()
{
   char str[1000];
   FILE *fptr;
   fptr=fopen("test.txt","w");	
   if(fptr==NULL)
   {
      printf("Error in opening file!");
      exit(1);
   }
   printf("Input a sentence to the file:");
   fgets(str, sizeof str, stdin);
   fprintf(fptr,"%s",str);
   fclose(fptr);
   printf("\n Data stored successfully\n");
   return 0;
}

Output:

Input a sentence to the file: Welcome to lingarajtechhub
Data stored successfully.

Join Our Community

Join our WhatsApp Group To know more About Programming Language tips, tricks and knowledge about programming and how to start learning any programming language.

Gg IwdemKi4wVQCwxT6Q XhqFDAprBfp4 nP7CCmW1Wu5S72MgI6ZgbAIYHSI6 8stz1khWD9m2FdhTF BeM3KTOT9EQrDpE0xypc7WabD BDBuSjA3yiBNK7PVLXXZbi1TMLA

Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 40

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply