training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

hardware.c (6079B)


      1 /* Exercise 11.12 */
      2 
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 #define REC 100
      8 #define LEN 30
      9 
     10 struct hwlist {
     11    int id;
     12    char product[LEN];
     13    int quantity;
     14    double price;
     15 };
     16 
     17 int main(void)
     18 {
     19    FILE *fd;
     20    struct hwlist blank_t = { 0, "blank", 0, 0.0 };
     21    struct hwlist list_t = blank_t;
     22    char string[LEN];
     23    int i;
     24 
     25    /* open the file */
     26    if( (fd = fopen("hardware.dat", "w")) == NULL ) {
     27       printf("cannot open the file %s\n", "hardware.dat");
     28       exit(-1);
     29    }
     30 
     31    /* create the empty records */
     32    for(i = 1; i <= REC; i++) {
     33       list_t.id = i;
     34       fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     35    }
     36 
     37    /*   S T A R T   O F   B A S E   D A T A   */
     38 
     39    list_t.id = 3;
     40    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     41    strncpy(list_t.product, "Smerigliatrice elettrica", 25);
     42    list_t.quantity = 7;
     43    list_t.price = 57.98;
     44    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     45 
     46    list_t.id = 17;
     47    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     48    strncpy(list_t.product, "Martello", 9);
     49    list_t.quantity = 76;
     50    list_t.price = 11.99;
     51    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     52 
     53    list_t.id = 24;
     54    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     55    strncpy(list_t.product, "Sega da traforo", 16);
     56    list_t.quantity = 21;
     57    list_t.price = 11.00;
     58    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     59 
     60    list_t.id = 39;
     61    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     62    strncpy(list_t.product, "Falciatrice", 12);
     63    list_t.quantity = 3;
     64    list_t.price = 79.50;
     65    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     66 
     67    list_t.id = 56;
     68    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     69    strncpy(list_t.product, "Sega elettrica", 15);
     70    list_t.quantity = 18;
     71    list_t.price = 99.99;
     72    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     73 
     74    list_t.id = 68;
     75    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     76    strncpy(list_t.product, "Giravite", 9);
     77    list_t.quantity = 106;
     78    list_t.price = 6.99;
     79    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     80 
     81    list_t.id = 77;
     82    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     83    strncpy(list_t.product, "Martello da fabbro", 19);
     84    list_t.quantity = 11;
     85    list_t.price = 21.50;
     86    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     87 
     88    list_t.id = 83;
     89    fseek(fd, (list_t.id - 1) * sizeof(struct hwlist), SEEK_SET);
     90    strncpy(list_t.product, "Chiave inglese", 15);
     91    list_t.quantity = 34;
     92    list_t.price = 7.50;
     93    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
     94 
     95    /*   E N D   O F   B A S E   D A T A   */
     96 
     97    freopen("hardware.dat", "r+", fd);
     98 
     99    while(1) {
    100       printf( /* Menu */
    101          "\nChoose an action\n\n"
    102          "\t1. Edit a record\n"
    103          "\t2. Delete a record\n"
    104          "\t3. Show all records\n"
    105          "\t4. Exit\n"
    106 	 "\nChoose: "
    107       ); /* end menu */
    108       scanf("%d", &i);
    109 
    110       if(i == 4)
    111 	 break;
    112 
    113       printf("\n");
    114 
    115       switch(i)
    116       {
    117 	 case 1: /* Edit a record */
    118 	    printf("Insert a product ID: ");
    119 	    scanf("%d", &i);
    120 
    121 	    printf("\n");
    122 
    123 	    fseek(fd, (i - 1) * sizeof(struct hwlist), SEEK_SET); 
    124 	    fread(&list_t, sizeof(struct hwlist), 1, fd);
    125 
    126 	    printf("ID: %d\nName: %s\nQuantity: %d\nPrice: %.2f\n\n",
    127 	       list_t.id, list_t.product, list_t.quantity, list_t.price);
    128 
    129 	    printf("Insert the new data or '-1' to leave unchanged\n\n");
    130 
    131 	    printf("ID: %d (unchangable)\n", list_t.id);
    132 	    printf("Name [%s]: ", list_t.product);
    133 	    fscanf(stdin, "%s", string);
    134 
    135 	    if( strncmp(string, "-1", 2) ) {
    136 	       strncpy(list_t.product, string, sizeof(list_t.product));
    137 	    }
    138 
    139 	    printf("Quantity [%d]: ", list_t.quantity);
    140 	    fscanf(stdin, "%s", string);
    141 	    if( strncmp(string, "-1", 2) ) {
    142 	       list_t.quantity = atoi(string);
    143 	    }
    144 
    145 	    printf("Price [%.2f]: ", list_t.price);
    146 	    fscanf(stdin, "%s", string);
    147 
    148 	    if( memcmp(string, "-1", 2) ) {
    149 	       list_t.price = atof(string);
    150 	    }
    151 
    152 	    fseek(fd, -1 * sizeof(struct hwlist), SEEK_CUR); 
    153 	    fwrite(&list_t, sizeof(struct hwlist), 1, fd);
    154 
    155 	    break;
    156          case 2: /* Delete a record */
    157 	    printf("Insert a product ID: ");
    158 	    scanf("%d", &i);
    159 
    160 	    printf("\n");
    161 
    162 	    fseek(fd, (i - 1) * sizeof(struct hwlist), SEEK_SET); 
    163 	    fread(&list_t, sizeof(struct hwlist), 1, fd);
    164 
    165 	    if(
    166 	       !memcmp(list_t.product, "blank", 5) ||
    167 	       list_t.quantity
    168 	    ) {
    169 	       printf("Cannot delete product number %d.\n", list_t.id);
    170 	       break;
    171 	    }
    172 
    173 	    printf("ID: %d\nName: %s\nQuantity: %d\nPrice: %.2f\n\n",
    174 	       list_t.id, list_t.product, list_t.quantity, list_t.price);
    175 
    176 	    printf("Are you sure (y/n)?: ");
    177 	    fscanf(stdin, "%s", string);
    178 
    179 	    printf("\nProduct number %d has ", list_t.id);
    180 
    181 	    if( !memcmp(string, "y", strlen(string)) ) {
    182 	       list_t = blank_t;
    183 	       list_t.id = i;
    184 	       fseek(fd, -1 * sizeof(struct hwlist), SEEK_CUR);
    185 	       fwrite(&list_t, sizeof(struct hwlist), 1, fd);
    186 	    }
    187 	    else
    188 	       printf("NOT ");
    189 
    190 	    printf("been deleted.\n");
    191 
    192 	    break;
    193 	 case 3: /* Show all records */
    194 	    rewind(fd);
    195 
    196 	    printf("\n%-10s%-30s%-15s%-10s\n",
    197 	       "ID", "Product", "Quantity", "Price");
    198 
    199 	    for(i = 0; i < 60; i++)
    200 	       printf("-");
    201 	    printf("\n");
    202 
    203 	    i = 0;
    204 	    fread(&list_t, sizeof(struct hwlist), 1, fd);
    205 	    while( !feof(fd) ) {
    206 	       if( memcmp(list_t.product, "blank", 5) ) { 
    207 	          printf("%-10d%-30s%-15d%-10.2f\n",
    208 		     list_t.id, list_t.product, list_t.quantity, list_t.price);
    209 	       }
    210 	       else {
    211 	          fread(&list_t, sizeof(struct hwlist), 1, fd);
    212 		  continue;
    213 	       }
    214 	       fread(&list_t, sizeof(struct hwlist), 1, fd);
    215 
    216 	       if( !(++i % 10) ) {
    217 		  if(i == 10) getchar();
    218 		  printf(
    219 		     "\nPress return to continue "
    220 		     "(or q and return to end)..\n"
    221 		  );
    222 		  string[0] = getchar();
    223 
    224 		  if(string[0] == 'q')
    225 		     break;
    226 	       }
    227 	    }
    228 
    229 	    printf("\n");
    230 
    231 	    break;
    232 	 default:
    233 	    printf("Invalid choose, please retry.\n");
    234       }
    235 
    236    }
    237 
    238    fclose(fd);
    239 
    240    return 0;
    241 } /* E0F main */
    242