improved formatting
This commit is contained in:
parent
d4af5c48af
commit
f3ab5d2efd
BIN
vendorInfo
BIN
vendorInfo
Binary file not shown.
34
vendorInfo.c
34
vendorInfo.c
|
@ -24,6 +24,7 @@ struct vendor_item
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char file_name[MAX_FILENAME_LENGTH];
|
char file_name[MAX_FILENAME_LENGTH];
|
||||||
|
printf("Enter name of input file: ");
|
||||||
read_input(file_name, MAX_FILENAME_LENGTH);
|
read_input(file_name, MAX_FILENAME_LENGTH);
|
||||||
|
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
|
@ -42,13 +43,13 @@ int main()
|
||||||
char *file_buffer = (char *) malloc(file_size);
|
char *file_buffer = (char *) malloc(file_size);
|
||||||
int index = 0;
|
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, " ");
|
char *token = strtok(file_buffer, " ");
|
||||||
strcpy(vendors[index].vendor_name, token);
|
strncpy(vendors[index].vendor_name, token, strlen(token) + 1);
|
||||||
|
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
strcpy(vendors[index].merchandise, token);
|
strncpy(vendors[index].merchandise, token, strlen(token) + 1);
|
||||||
|
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
vendors[index].inventory_count = atoi(token);
|
vendors[index].inventory_count = atoi(token);
|
||||||
|
@ -56,14 +57,29 @@ int main()
|
||||||
token = strtok(NULL, " ");
|
token = strtok(NULL, " ");
|
||||||
vendors[index].item_price_baht = atoi(token);
|
vendors[index].item_price_baht = atoi(token);
|
||||||
|
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < MAX_VENDORS; i++)
|
int total_vendors = index - 1;
|
||||||
{
|
printf("\n>>> Read %d vendors from file %s\n", total_vendors, file_name);
|
||||||
printf("%d | %s %s %d %d\n", i, vendors[i].vendor_name, vendors[i].merchandise, vendors[i].inventory_count, vendors[i].item_price_baht);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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);
|
fclose(fp);
|
||||||
free(file_buffer);
|
free(file_buffer);
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -83,4 +99,4 @@ int read_input(char str[], int n)
|
||||||
|
|
||||||
str[i] = '\0';
|
str[i] = '\0';
|
||||||
return i;
|
return i;
|
||||||
}
|
};
|
||||||
|
|
Loading…
Reference in New Issue