Thread: Where is the mistake in my code?

  1. #1
    Registered User
    Join Date
    Oct 2020
    Posts
    69

    Where is the mistake in my code?

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,714
    Well we don't have your data file, so have you run the code in a debugger?

    Put a breakpoint on
    > while(curr->next != NULL)
    and run the code.

    The debugger will stop the code when it gets to that line.
    From there, you can examine variables, single step the code, set more breakpoints.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2020
    Posts
    69
    I managed to solve the issue. I was using fixed sizes for region and country and I think they were to small. I replaced them with dynamically allocated arrays and it's working

  4. #4
    Registered User
    Join Date
    May 2021
    Posts
    19
    Quote Originally Posted by rmmstn View Post
    I managed to solve the issue. I was using fixed sizes for region and country and I think they were to small. I replaced them with dynamically allocated arrays and it's working

    I think it's worth making sure what the problem was. A problem left behind, with a solution that is not understood, will often come back to bite you. That's my experience, anyway. Good luck!
    "Who cares, wins"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can`t find mistake in this code
    By AidenK in forum C Programming
    Replies: 4
    Last Post: 05-25-2016, 12:59 PM
  2. can anyone help to spot mistake in the code
    By chess_queen in forum C Programming
    Replies: 1
    Last Post: 10-21-2012, 10:37 AM
  3. Can not find my mistake in this code?
    By BLG in forum C Programming
    Replies: 2
    Last Post: 09-04-2009, 04:19 PM
  4. What is the mistake in following code?
    By shwetha_siddu in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 02:43 AM
  5. where is my mistake following this code..
    By transgalactic2 in forum C Programming
    Replies: 9
    Last Post: 10-18-2008, 01:44 PM

Tags for this Thread