putting data into array of struct

This commit is contained in:
Win 2024-08-29 11:31:21 +07:00
parent 32f7ae0007
commit c182be9758
2 changed files with 23 additions and 2 deletions

Binary file not shown.

View File

@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VENDORS 20
#define MAX_FILENAME_LENGTH 4096
@ -37,10 +38,30 @@ int main()
unsigned long file_size = ftell(fp);
fseek(fp, 0, SEEK_SET);
// read file to buffer
char *file_buffer = (char *) malloc(file_size);
fread(file_buffer, file_size, 1, fp);
int index = 0;
printf("%s", file_buffer);
while (fgets(file_buffer, 50, fp) != NULL)
{
char *token = strtok(file_buffer, " ");
strcpy(vendors[index].vendor_name, token);
token = strtok(NULL, " ");
strcpy(vendors[index].merchandise, token);
token = strtok(NULL, " ");
vendors[index].inventory_count = atoi(token);
token = strtok(NULL, " ");
vendors[index].item_price_baht = atoi(token);
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);
}
fclose(fp);
free(file_buffer);