Modify

Can any one help me
in function modify i wanna modify the data but i cannt do it


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
#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 modify (char* id)
{
       FILE * search ;
       search = fopen("Assignment_3.txt","a") ;
       fread(&s1 , sizeof(s1) , 1 , search ) ;
       while(!feof(search))
       {
            if ( strcmp ( id , s1.id) == 0 )
            {
                 //fseek(search,2,2);
                 printf("\n This is the old data :\n");

                 printf("id: %s \n",s1.id);
                 printf("First Name : %s \n",s1.fName);
                 printf("Last Name : %s \n",s1.lName);
                 printf("Last Name : %s \n",s1.dept);
                 printf("gpa: %s \n",s1.gpa);        
                 printf("level: %s \n",s1.level);

                 printf("Enter Student ID : ") ;
                 scanf("%s",s1.id);
                 fwrite(s1.id , sizeof(s1.id) , 1 , search ) ;
/*                 printf("Enter Student First Name : ") ;
                 scanf("%s",s1.fName);
                 printf("Enter Student Last Name : ") ;
                 scanf("%s",s1.lName);
                 printf("Enter Student Department : ") ;
                 scanf("%s",s1.dept);
                 printf("Enter Student GPA : ") ;
                 scanf("%s",s1.gpa);
                 printf("Enter Student Level : ") ;
                 scanf("%s",s1.level);
                 
                 fwrite(&s1 , sizeof(s1) , 1 , search ) ; */
            }
       fread(&s1 , sizeof(s1) , 1 , search ) ;


       }
       fclose(search);       
}

struct Student search(char* id)
{
       FILE * search ;
       search = fopen("Assignment_3.txt","r") ;
       fread(&s1 , sizeof(s1) , 1 , search ) ;
       while(!feof(search))
       {
            if ( strcmp ( id , s1.id) == 0 )
            {
                 printf("id: %s \n",s1.id);
                 printf("First Name : %s \n",s1.fName);
                 printf("Last Name : %s \n",s1.lName);
                 printf("gpa: %s \n",s1.gpa);
                 printf("level: %s \n",s1.level);
            }
       fread(&s1 , sizeof(s1) , 1 , search ) ;
       }
       fclose(search);       
}
int main()
{
    char y ;
    char ID[10] ;
    
    FILE *risha;

    if(risha == NULL) 
    {
          printf("Can't Find The File. \n") ;
          exit(1) ;
    }
    else
    {
              printf("1- Add Another Student.\n") ;
              printf("2- Add Student At begin of file.\n") ;
              printf("3- Search For Student.\n") ;
              printf("4- Show All Students.\n") ;
              printf("5- Modify Student.\n") ;
              printf("7- Exit.\n") ;
              printf("Enter Your Choice : ") ;
              scanf("%s",&y) ;
              while ( y != 7 )
              {
                    switch (y) 
                    {
                        case '1' : // Add Another Student
                                  {
                                        risha=fopen("Assignment_3.txt","a");

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

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

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

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

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

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

                                        fwrite(&s , sizeof(s) , 1 , risha ) ;
                                        fclose(risha) ;
                                        break ;
                                  } // end case 1
                         case '2' : //Add Student At begin of file
                                  {
                                      risha=fopen("Assignment_3.txt","w");

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

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

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

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

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

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

                                        fwrite(&s , sizeof(s) , 1 , risha ) ;
                                        fclose(risha) ;
                                        break ;
                                  } // end case 2
                         case '3' : // search
                                  {
                                        printf("Enter Student ID : ") ;
                                        scanf("%s",ID) ;
                                        search ( ID ) ;
                                        break ;
                                  } // end case 3
                         case '4' : // display all students
                                  {
                                  risha = fopen("Assignment_3.txt","r");

                                  while(!feof(risha))
                                  {
                                    fread(&s1 , sizeof(s1) , 1 , risha ) ;
                                    printf("id: %s \n",s1.id);
                                    printf("First Name : %s \n",s1.fName);
                                    printf("Last Name : %s \n",s1.lName);
                                    printf("gpa: %s \n",s1.gpa);
                                    printf("level: %s \n",s1.level);
                                  }
                                  fclose(risha);
                                  break ;
                                  } // end case 4 
                          case '5' :
                                   {
                                        printf("Enter Student ID : ") ;
                                        scanf("%s",ID) ;
                                        modify ( ID ) ;
                                        break ;
                                   } // end case 5
                          case '7' :
                                   {
                                        system("pause");
                                        return 0 ;
                                   }
                    } // end switch 
              printf("\n1- Add Another Student.\n") ;
              printf("2- Add Student At begin of file.\n") ;
              printf("3- Search For Student.\n") ;
              printf("4- Show All Students.\n") ;
              printf("5- Modify Student.\n") ;
              printf("7- Exit.\n") ;
              printf("Enter Your Choice : ") ;
              scanf("%s",&y) ;
              } // end while 
} // end else 
              system("pause") ;
              return 0 ;
}
Topic archived. No new replies allowed.