Я новичок в C и пытаюсь распечатать содержимое csv-файла. Файл содержит 2 столбца и 6 строк. Первая строка содержит имя клиента и бюджет, и я могу распечатать его. Остальные строки содержат название продукта и количество, которое я хотел бы купить. Я думаю, что проблема может быть с моим для l oop, так как он не проходит мимо первого ряда. Я занимаюсь этим уже несколько дней.
Буду признателен за любую помощь
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
struct Customer {
char* name;
double budget;
char* product[5];
int quantity;
int index;
};
struct Customer custOrder()
{
FILE * fp;
char * line = NULL;
size_t len = 0;
size_t read;
fp = fopen("customerkim.csv", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
read = getline(&line, &len, fp);
char *a = strtok(line, ",");
char *b = strtok(NULL, ",");
char *custName = malloc(sizeof(char) * 50);
double custBudget = atof(b);
strcpy(custName, a);
while((read = getline(&line, &len, fp)) != -1) {
char *c = strtok(line, ",");
char *d = strtok(NULL, ",");
int custQuantity = atoi(d);
char * custProduct = malloc(sizeof(char) * 50);
strcpy(custProduct, c);
struct Customer customer = {custName, custBudget, custProduct, custQuantity};
struct Customer productItem = {product};
customer.product[customer.index++] = productItem;
}
return customer;
}
void printCustomer(struct Customer c)
{
printf("CUSTOMER NAME: %s \nCUSTOMER BUDGET: %.2f\n", c.name, c.budget);
printf("___________________\n");
for(int i = 0; i < c.index; i++)
{
struct Customer product = c.product[i]
printf("%s ORDERS %d of %s\n", c.name, c.quantity, c.product);
}
}
int main(void)
{
struct Customer customer = custOrder();
printCustomer(customer);
return 0;
}