/********************************************************* * From C PROGRAMMING: A MODERN APPROACH, Second Edition * * By K. N. King * * Copyright (c) 2008, 1996 W. W. Norton & Company, Inc. * * All rights reserved. * * This program may be freely distributed for class use, * * provided that this copyright notice is retained. * *********************************************************/ /* invclear.c (Chapter 22, page 574) */ /* Modifies a file of part records by setting the quantity on hand to zero for all records */ #include #include #define NAME_LEN 25 #define MAX_PARTS 100 struct part { int number; char name[NAME_LEN+1]; int on_hand; } inventory[MAX_PARTS]; int num_parts; int main(void) { FILE *fp; int i; if ((fp = fopen("inventory.dat", "rb+")) == NULL) { fprintf(stderr, "Can't open inventory file\n"); exit(EXIT_FAILURE); } num_parts = fread(inventory, sizeof(struct part), MAX_PARTS, fp); for (i = 0; i < num_parts; i++) inventory[i].on_hand = 0; rewind(fp); fwrite(inventory, sizeof(struct part), num_parts, fp); fclose(fp); return 0; }