C Program to create a File & write Data in it
Below is a program to create a new file and then storing information in it.
/* open for writing */
fptr = fopen("emp.txt", "w");
if (fptr == NULL)
printf("File does not exist.\n");
printf("Enter the name:\n");
fprintf(fptr, "Name = %s\n", name);
printf("Enter the age:\n");
fprintf(fptr, "Age = %d\n", age);
printf("Enter the salary:\n");
fprintf(fptr, "Salary = %.2f\n", salary);
You can add any information in the file, like we have added Name, Age and Salary for some employees, you can change the program as per your requirements.
You can even initialise a
for loop, to add details of multiple employees to the file. All you have to do is, ask user for number of employees for which data has to be stored, run the
for loop that many times and keep on adding the data to the file.