C Program to create file and store information?

All QuestionsCategory: C LanguageC Program to create file and store information?
chetan shidling asked 4 weeks ago

I need code.

1 Answers
chetan shidling answered 4 weeks ago

Code to create file and store information:
 
#include <stdio.h>
#include <stdlib.h>
void main()
{
FILE *fptr;
char name[20];
int age, n, i, salary;
// clrscr();
// Opening file for writing
fptr = fopen(“C:\\Users\\HP\\Desktop\\ds\\file.txt”, “w”);
if(fptr == NULL)
{
printf(“File does not exists \n”);
return;
}
printf(“Number of Users you want to enter : “);
scanf(“%d”, & n);
for(i=1;i<=n;i++)
{
printf(“Person Details\n”);
printf(“Enter the name\n”);
gets(name);
fprintf(fptr, “Name = %s\n”, name);
printf(“Enter the age\n”);
scanf(“%d”, &age);
fprintf(fptr,”Age = %d\n”, age);
printf(“Enter the salary \n”);
scanf(“%d”, & salary);
fprintf(fptr,”Salary = %d\n”, salary);
printf(“\n”);
}
fclose(fptr);
getch();
return 0;
}
 
Output:
 
Number of Users you want to enter: 2
Person Details
Enter the name
Chetan
Enter the age
21
Enter the salary
1000000
Person Details
Enter the name
Akshay
Enter the age
54
Enter the salary
212345

Your Answer

0 + 4 =