I have a CSV file of the following format:
Region, Country, Item Type, Sales Channel, Order Priority, Order Date, Order ID, Ship Date, Units, Sold, Unit Price, Unit Cost, Total Revenue, Total Cost, Total Profit
I need to extract Region, Country, Total Profit and Total Cost and organize them in an ordered singly linked list.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define MAX 1024




typedef struct node{
    char region[50], country[50];
    double profit, cost;
    struct node *next;
}node;


void insert_beginning(node **root, char region_aux[50], char country_aux[50], double profit_aux, double cost_aux)
{
    node *new_node = malloc(sizeof(node));


    if(new_node == NULL)
    {
        fprintf(stderr, "Error allocating memory\n");
        exit(EXIT_FAILURE);
    }


    strcpy(new_node->region, region_aux);
    strcpy(new_node->country, country_aux);
    new_node->profit = profit_aux;
    new_node->cost = cost_aux;
    new_node->next = *root;
    *root = new_node;
}


void insert_after(node *AfterNode, char region_aux[50], char country_aux[50], double profit_aux, double cost_aux)
{
    node *new_node = malloc(sizeof(node));
    
    if(new_node == NULL)
    {
        fprintf(stderr, "Error at memory allocation\n");
        exit(EXIT_FAILURE);
    }


    strcpy(new_node->region, region_aux);
    strcpy(new_node->country, country_aux);
    new_node->profit = profit_aux;
    new_node->cost = cost_aux;
    new_node->next = AfterNode->next;
    AfterNode->next = new_node;
}


void insert_sorted(node **root, char region_aux[50], char country_aux[50], double profit_aux, double cost_aux)
{
    if(*root == NULL || strcmp((**root).region, region_aux) >= 0)
    {
        insert_beginning(root, region_aux, country_aux, profit_aux, cost_aux);
        return;
    }
    node *curr = *root;
    while(curr->next != NULL)
    {
        if(strcmp(curr->next->region, region_aux) > 0)
        {
            insert_after(curr, region_aux, country_aux, profit_aux, cost_aux);
            return;
        } 
        else if(strcmp(curr->next->region, region_aux) < 0)
        {
            curr = curr->next;
        }
        else if(strcmp(curr->next->region, region_aux) == 0)
        {
            if(strcmp(curr->next->country, country_aux) > 0)
            {
                insert_after(curr, region_aux, country_aux, profit_aux, cost_aux);
                return;
            }
            else
                curr = curr->next;
        }
    }
    insert_after(curr, region_aux, country_aux, profit_aux, cost_aux);
}


void deallocate(node **root)
{
    node *curr = *root;
    while(curr != NULL)
    {
        node *aux = curr;
        curr = curr->next;
        free(aux);
    }
    *root = NULL;
}


int main(int argc, char **argv)
{
    FILE *fin = fopen(argv[1], "r"), *fout = fopen(argv[2], "w");
    node *root = NULL;
    char buf[MAX];


    if(!fin)
    {
        fprintf(stderr, "Error opening file '%s' for reading.\n", argv[1]);
        exit(EXIT_FAILURE);
    }
    if(!fout)
    {
        fprintf(stderr, "Error opening file '%s' for writing.\n", argv[2]);
        exit(EXIT_FAILURE);
    }


    if(argc != 3)
    {
        fprintf(stderr, "Invalid format. Correct usage: %s input_file.csv output_file.csv\n", argv[0]);
        exit(EXIT_FAILURE);
    }


    while(fgets(buf, MAX, fin))
    {
        buf[strcspn(buf, "\n")] = '\0';  // strip the trailing newline
        char *fields[15];
        char *word = strtok(buf, ",");
        int i = 0;


        while(word)
        {
            fields[i++] = word;
            word = strtok(NULL,",");
        }


        insert_sorted(&root, fields[0], fields[1], atof(fields[13]), atof(fields[12])); 
    }


    for(node *curr = root; curr != NULL; curr = curr->next)
    {
        printf("%s - %s - %lf - %lf\n", curr->region, curr->country, curr->cost, curr->profit);
    }


    fclose(fin);
    fclose(fout);
    deallocate(&root);
    return 0;
}
When I run this code with the proper arguments, nothing happens, not even a seg fault it just keeps running with no result. What am I missing?