Search in C using Files

can any one help me in search function
it didn't work can anyone help me
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <stdio.h>
#include <stdlib.h>

struct Student
{
  char id[10];
  char fName[25];
  char lName[25];
  char dept[10];
  char level[5];
  char gpa[5];
  char grade[15];
}  s , s1 ;

struct Student search(struct Student s1)
{
       char* c ;
       FILE * search ;
       search = fopen("Assignment_3.txt","r");
       while( ( c = getchar() ) != EOF ) 
       {
               if ( c == s1.id )
               {
                   fread(&s1,sizeof(s1),1,search) ;
               }
               else 
               printf("ID not found.\n");
       }
       fclose(search);
}

int main()
{
    char y ;
    
    FILE *risha;
    risha=fopen("Assignment_3.txt","w");

    if(risha == NULL) 
    {
          printf("Can't Find The File. \n") ;
          exit(1) ;
    }
    else
    {
              printf("Enter Student ") ;
              printf(" ID:");
              scanf("%s",s.id);

              printf("Enter Student ") ;
              printf(" First Name : ");
              scanf("%s",s.fName);

              printf("Enter Student ") ;
              printf(" Last Name : ");
              scanf("%s",s.lName);

              printf("Enter Student ") ;
              printf(" Department : ");
              scanf("%s",s.dept);

              printf("Enter Student ") ;
              printf(" GPA : ");
              scanf("%s",s.gpa);

              printf("Enter Student ") ;
              printf(" Level : ");
              scanf("%s",s.level);

              printf("Enter Student ") ;
              printf(" Grade : ");
              scanf("%s",s.grade);

              fwrite(&s , sizeof(s) , 1 , risha ) ;

              fclose(risha);

              printf("1- Add Another Student.\n") ;
              printf("2- Search For Student.\n") ;
              printf("3- Show All Students.\n") ;
              printf("4- Exit.\n") ;
              printf("Enter Your Choice : ") ;
              scanf("%s",&y) ;

              while ( y == '4' )
              {
                   exit(1);
              }
              while ( y == '2' )
              {
                 printf("Enter Student ID : ") ;
                 scanf("%s",s1.id) ;
                 search ( s1 ) ;
                 printf("1- Add Another Student.\n") ;
                 printf("2- Search For Student.\n") ;
                 printf("3- Show All Students.\n") ;
                 printf("4- Exit.\n") ;
                 printf("Enter Your Choice : ") ;
                 scanf("%s",&y) ;
              }

              while ( y == '3' )
              {
               int c;
               risha = fopen("Assignment_3.txt","r");
               c = getc(risha) ;
               while (c!= EOF)
                {
   		            putchar(c);
                    c = getc(risha);
                 }
                fclose(risha);

                printf("1- Add Another Student.\n") ;
                printf("2- Search For Student.\n") ;
                printf("3- Show All Students.\n") ;
                printf("4- Exit.\n") ;
                printf("Enter Your Choice : ") ;
                scanf("%s",&y) ;
              }

              while ( y == '1' )
              {
              risha=fopen("Assignment_3.txt","a");

              printf("Enter Student ") ;
              printf(" ID:");
              scanf("%s",s.id);

              printf("Enter Student ") ;
              printf(" First Name : ");
              scanf("%s",s.fName);

              printf("Enter Student ") ;
              printf(" Last Name : ");
              scanf("%s",s.lName);

              printf("Enter Student ") ;
              printf(" Department : ");
              scanf("%s",s.dept);

              printf("Enter Student ") ;
              printf(" GPA : ");
              scanf("%s",s.gpa);

              printf("Enter Student ") ;
              printf(" Level : ");
              scanf("%s",s.level);

              printf("Enter Student ") ;
              printf(" Grade : ");
              scanf("%s",s.grade);

              fwrite(&s , sizeof(s) , 1 , risha ) ;

              printf("1- Add Another Student.\n") ;
              printf("2- Search For Student.\n") ;
              printf("3- Show All Students.\n") ;
              printf("4- Exit.\n") ;
              printf("Enter Your Choice : ") ;
              scanf("%s",&y) ;

              while ( y == '4' )
              {
                   exit(1);
              }
              while ( y == '2' )
              {
                 printf("Enter Student ID : ") ;
                 scanf("%s",s1.id) ;
                 search ( s1 ) ;

                 printf("1- Add Another Student.\n") ;
                 printf("2- Search For Student.\n") ;
                 printf("3- Show All Students.\n") ;
                 printf("4- Exit.\n") ;
                 printf("Enter Your Choice : ") ;
                 scanf("%s",&y) ;
              }

              while ( y == '3' )
              {
               int c;
               risha = fopen("Assignment_3.txt","r");
               c = getc(risha) ;
               while (c!= EOF)
                {
   		            putchar(c);
                    c = getc(risha);
                 }
                fclose(risha);

                printf("1- Add Another Student.\n") ;
                printf("2- Search For Student.\n") ;
                printf("3- Show All Students.\n") ;
                printf("4- Exit.\n") ;
                printf("Enter Your Choice : ") ;
                scanf("%s",&y) ;
              }
          }
    }
    system("pause");
    return 0;
}
1
2
3
4
5
6
7
8
9
       while( ( c = getchar() ) != EOF ) 
       {
               if ( c == s1.id )
               {
                   fread(&s1,sizeof(s1),1,search) ;
               }
               else 
               printf("ID not found.\n");
       }

This reads a character, then you read a structure starting one char on. You're reading from the wrong position. In C++, you really should be using the iostream library, as it's so much more flexible, but if you want to test for EOF, go ahead and use fread(), but check the return code.
Topic archived. No new replies allowed.