C program to get data from user and store it into the file

In these tutorial we will learn how to input data from user and store it into the file...


EX: Write a program in C to create and store information in a text file, How to write C program to get data from user and print it on the output screen, C program to write data into a file.


Program to get data from user and store it into the file:

#include<stdio.h> #include<stdlib.h> int main() { FILE *fp; char str[500]; fp = fopen("file.txt", "w"); if(fp==NULL) { printf("Error! File not created:("); exit(1); } //Get data from user using fgets() printf("Enter data to store in file:\n"); fgets(str, 500,stdin); //store data in file fputs(str,fp); printf("Data stored successfully into the file..."); fclose(fp); return 0; }


Above program shows the following output:

Enter data to store in file: 

File handling is easy....

Data stored successfully into the file...


When you open the file(file.txt), you can see the data you entered:

    Related Articles:


No comments:

Post a Comment

If you have any doubts, please discuss here...👇