diff --git a/vendorInfo b/vendorInfo index 7b2cb41..93fa37d 100755 Binary files a/vendorInfo and b/vendorInfo differ diff --git a/vendorInfo.c b/vendorInfo.c index 65347c1..1c48d69 100644 --- a/vendorInfo.c +++ b/vendorInfo.c @@ -24,6 +24,7 @@ struct vendor_item int main() { char file_name[MAX_FILENAME_LENGTH]; + printf("Enter name of input file: "); read_input(file_name, MAX_FILENAME_LENGTH); FILE *fp; @@ -42,13 +43,13 @@ int main() char *file_buffer = (char *) malloc(file_size); int index = 0; - while (fgets(file_buffer, 50, fp) != NULL) + while ((fgets(file_buffer, 50, fp) != NULL) && (index <= MAX_VENDORS)) { char *token = strtok(file_buffer, " "); - strcpy(vendors[index].vendor_name, token); + strncpy(vendors[index].vendor_name, token, strlen(token) + 1); token = strtok(NULL, " "); - strcpy(vendors[index].merchandise, token); + strncpy(vendors[index].merchandise, token, strlen(token) + 1); token = strtok(NULL, " "); vendors[index].inventory_count = atoi(token); @@ -56,14 +57,29 @@ int main() token = strtok(NULL, " "); vendors[index].item_price_baht = atoi(token); - index++; + index++; } - for (int i = 0; i < MAX_VENDORS; i++) - { - printf("%d | %s %s %d %d\n", i, vendors[i].vendor_name, vendors[i].merchandise, vendors[i].inventory_count, vendors[i].item_price_baht); - } + int total_vendors = index - 1; + printf("\n>>> Read %d vendors from file %s\n", total_vendors, file_name); + for (int i = 0; i <= total_vendors; i++) + { + char vendor_info[200]; + sprintf(vendor_info, "%s sells %s for %d each", + vendors[i].vendor_name, + vendors[i].merchandise, + vendors[i].item_price_baht + ); + + char vendor_stock[50]; + sprintf(vendor_stock, "Inventory %d items", + vendors[i].inventory_count + ); + + printf("%-50s %s\n", vendor_info, vendor_stock); + } + fclose(fp); free(file_buffer); return 0; @@ -83,4 +99,4 @@ int read_input(char str[], int n) str[i] = '\0'; return i; -} +};