shop.c
// Struct to create and stock shop
Struct to create and stock shop
Struct to create and stock shop
struct Shop createAndStockShop() 
{    
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    ssize_t read;
    fp = fopen("csv_files/stock.csv", "r");
    if (fp == NULL)
        exit(EXIT_FAILURE);
    read = getline(&line, &len, fp);
    float cash = atof(line);
    printf("CASH IN SHOP IS %.2f\n", cash);
    struct Shop shop = { cash };
    while ((read = getline(&line, &len, fp)) != -1) {
        char *n = strtok(line, ",");
        char *p = strtok(NULL, ",");
        char *q = strtok(NULL, ",");
        double price = atof(p); 
        int quantity = atoi(q); 
        char *name = malloc(sizeof(char) * 50); 
        strcpy(name, n); 
        struct Product product = { name, price };
        struct ProductStock stockItem = { product, quantity };
        shop.stock[shop.index++] = stockItem;
    }
    return shop;
}
struct Shop createAndStockShop ( ) { FILE * fp ; char * line = NULL ; size_t len = 0 ; ssize_t read ; fp = fopen ( " " , " " ) ; if ( fp == NULL ) exit ( EXIT_FAILURE ) ; read = getline ( & line , & len , fp ) ; float cash = atof ( line ) ; printf ( " \n " , cash ) ; struct Shop shop = { cash } ; while ( ( read = getline ( & line , & len , fp ) ) != -1 ) { char * n = strtok ( line , " " ) ; char * p = strtok ( NULL , " " ) ; char * q = strtok ( NULL , " " ) ; double price = atof ( p ) ; int quantity = atoi ( q ) ; char * name = malloc ( sizeof ( char ) * 50 ) ; strcpy ( name , n ) ; struct Product product = { name , price } ; struct ProductStock stockItem = { product , quantity } ; shop . stock [ shop . index ++ ] = stockItem ; } return shop ; }
Struct to create and stock shop
Struct to create and stock shop
// Opens csv with stock & shop value // Gets cash balance from csv // String tokeniser method // printf("Retrieved line of length %zu:\n", read); // converts to floating point // converts to integer // Dynamcally allocated new memory for holding product name // makes copy of data stored in n // Put data from CSV file into struct // adding items to shop // printf("NAME OF PRODUCT: %s\n PRICE: %.2f\n QUANTITY: %d\n\n", name, price, quantity);