-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile4.c
More file actions
57 lines (56 loc) · 1.29 KB
/
file4.c
File metadata and controls
57 lines (56 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Write a program to input student information from a user & enter it to a file.
#include<stdio.h>
typedef struct student
{
int roll;
char name[100];
int marks[5];
}stu;
void input(stu *a);
void fileinput(stu *a);
int main()
{
int noOfstudent;
printf("Enter number of students :");
scanf("%d",&noOfstudent);
stu stuData[noOfstudent];
for(int i=0;i<noOfstudent;i++)
{
input(&stuData[i]);
fileinput(&stuData[i]);
}
return 0;
}
void input(stu *a)
{
printf("Enter roll :");
scanf("%d",&a->roll);
printf("Enter name : ");
scanf(" %[^\n]s",a->name);
printf("Enter marks of 5 subjects below \n");
for(int i=0;i<5;i++)
{
printf("Enter marks of subject %d : ",i+1);
scanf("%d",&a->marks[i]);
if(a->marks[i]<0)
{
printf("Enter valid marks \n");
printf("Enter marks of subject %d : ",i+1);
scanf("%d",&a->marks[i]);
}
}
}
void fileinput(stu *a)
{
FILE *fptr;
fptr=fopen("file4.txt","a");
fprintf(fptr,"%d \t",a->roll);
fprintf(fptr,"%s \t",a->name);
for(int i=0;i<5;i++)
{
fprintf(fptr,"%d \t",a->marks[i]);
}
fprintf(fptr,"\n");
printf("Data recorded in file successfully \n");
fclose(fptr);
}