c - Structure data written into file is not being read correctly -
printItems () always prints the previously entered item when I choose to update the item details so that the file is updated Are doing but printItems () does not display the updated ones.
structure canteen {four item IDs [15]; Four names [15]; Int price; }; Zero print items () {FILE * fp1; FP1 = fopen ("items.txt", "R"); Structure canteen c; Printf ("\ t% -15s% -30s% -5s \ n", "item id", "item name", "value"); While (Fred (& amp; c, sizeof (struct canteen), 1, FP1)! = Null) {printf ("\ t% -15s% -30s% -5d \ n", c.itemId, c.name , C. Value); } Fclose (fp1); } Int updateItem (four id []) {FILE * fp; Int pos = 0; Fp = fopen ("items.bin", "r +"); En found = 0; Structure canteen b; While (Fred (and B, Size (Structure canteen), 1, FP) = = Faucet) {pos = ftell (fp); If (strcmp (b.itemId, id) == 0) {found = 1; Fasek (FP, -Secef (Structure canteen), 1); Printf ("Enter new item ID:"); Scanf ("% s", b.itemId); Printf ("Enter new item name:"); Scanf ("% s", b.name); Printf ("Enter new item value:"); Scanf ("% d", & amp; amp; b.price); FILIT (amp; amp; amp; b, sizeof (structure canteen), 1, FP); Fclose (fp); break; }} Fclose (fp); If (found == 1) return 1; Other {Return 0; }} Zero main () {FILE * fp1; File * FP2; Four s [1]; Four choices [1]; Four id [15]; Int n, i, j; Printf ("Enter the number of items \ n"); Scanf ("% d", & amp; n); FP1 = fopen ("items.bin", "w"); Structure canteen c; For (i = 0; i & lt; n; i ++) {printf ("Enter item id% d:", i + 1); Scanf ("% s", c.itemId); Printf ("Enter item name% d:", I + 1); Scanf ("% s", c.name); Printf ("Enter item% D value:", I + 1); Scanf ("% d", & amp; c.price); Fillit (& amp; c, size (structure canteen), 1, FP1); } Fclose (fp1); Printf ("The details of the item are stored in files.txt \ n"); PrintItems (); While (1) {printf ("Do you want to update the item? \ N enter y or n:"); Scanf ("% s", s); If (s [0]! = 'N') {printf ("Enter item ID:"); Scanf ("% s", id); If (! UpdateItem (id)) printf ("No item found with id:% s", id); Else {printf ("Item detials. \ N" after update); PrintItems (); }} And break; } Getch (); }
printItems () is always printing the previously entered item. When I choose to update item details, they are updating the file but printItems () does not display the updated person.
Comments
Post a Comment