/
STUDENTF.CPP
137 lines (104 loc) · 2.35 KB
/
STUDENTF.CPP
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
struct student
{
int rno;
char name[20];
int marks;
};
void fswrite(student *stud, FILE *ostream);
void fsread();
void seekNread(int rn);
void main(void)
{
clrscr();
student s1;
printf("enter Roll no:");
scanf("%d",&s1.rno);
flushall();
printf("enter Name");
scanf("%[^\n]", s1.name);
flushall();
printf("enter marks");
scanf("%d", &s1.marks);
flushall();
printf("\nread from console: %d-%s-%d",s1.rno, s1.name, s1.marks);
FILE *F;
if ((F = fopen("d:\\duplicat.txt", "a+b")) == NULL)
fprintf(stderr, "Cannot open input file.\n");
fswrite(&s1,F);
fclose(F);
fsread();
int roll;
printf("\nEnter roll number to find student record: ");
scanf("%d",&roll);
seekNread(roll);
}
void fswrite(student *stud, FILE *ostream)
{
// FILE *ostream;
/* printf("\nIn Write routine: %d-%s-%d",stud->rno, stud->name, stud->marks);
if ((ostream = fopen("d:\\duplicat.txt", "wb")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n");
}
*/
fwrite(&stud->rno, sizeof(int), 1, ostream); /* write struct s to file */
fwrite(&stud->name, 20*sizeof(char), 1, ostream);
fwrite(&stud->marks, sizeof(int), 1, ostream);
//while (!feof(in))
// fputc(fgetc(in), out);
// fclose(ostream);
}
void fsread()
{
FILE *istream;
student st;
if ((istream = fopen("d:\\duplicat.txt", "rb")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n");
}
// printf("Fseek call %d",fseek(istream, 0, 0));
while(!feof(istream))
{
fread(&st, sizeof(student), 1, istream); /* write struct s to file */
printf("\nRead from File: %d-%s-%d",st.rno, st.name, st.marks);
}
//while (!feof(in))
// fputc(fgetc(in), out);
fclose(istream);
}
void seekNread(int rn)
{
student stud;
FILE *istream;
long int k;
fpos_t fpos;
if ((istream = fopen("d:\\duplicat.txt", "rb")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n");
}
fseek(istream, 0L, SEEK_SET);
while(!feof(istream))
{
fread(&stud, sizeof(student), 1, istream); /* write struct s to file */
/* if(stud.rno==rn)
{
k = ftell(istream);
printf("\n%ld",k);
if(k!=0)
{
fpos = k - sizeof(student);
fsetpos(istream, &fpos);
break;
}
} */
if(stud.rno==rn)
{
printf("\nRead from File as desired: %d-%s-%d",stud.rno, stud.name, stud.marks);
break;
}
}
fclose(istream);
}