0

I read from a csv file only the title year and budget and then i want to put them in an ordered way after the year all the elements in a linked list. I store the title, year and the budget in an array of struct movies how could i put the elements in an ordered way in the list?

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct movies{
    unsigned int year;
    char title[101];
    unsigned int budget;
}*arr;
typedef struct movies node_t;
typedef struct node list;
struct node
{
  node_t value;
  struct node *next;
};

void  printlist(list *head)
{
    list *temp=head;
    while(temp!=NULL)
    {
         printf("Release year is : %d\n Title is : %s \n Budget was : %u \n -------\n ", temp->value.year,temp->value.title,temp->value.budget);
        temp = temp->next;
       // printf("%d-",content->next);
    }
}


int main(int argc,char* argv[])
{
    list *head,*tail;
    head=NULL;
    tail=NULL;
    if(argc<2)
    {
        printf("Not enough command line arguments \n");
        return 0;
    }
    FILE* f=fopen(argv[1],"r");
    if(!f)
    {
        printf("Couldn't open file\n");
        return 0;
    }
    char content[300];
    int nrlines=0;
    while(fgets(content,300,f)!=NULL)
      {
        nrlines++;
        // printf("da\n");
      }
    arr=(struct movies*) malloc (nrlines*sizeof(struct movies));
    int k=0;
    int i=0;
    char comma_title[50];
    fclose(f);
    f=fopen(argv[1],"r");
    fgets(content,300,f);
    while(fscanf(f,"%[^,],",content)==1)
    {

               if(content[0]=='\"')
          {
            fscanf(f,"%[^\"]",comma_title);
            strcat(content,",");
            strcat(content,comma_title);
            strcat(content,"\"");
            fgetc(f);
            fgetc(f);
          }
        if(k==0)
        {
            arr[i].year=atoi(content);
            //  printf("Year is:%d\n",atoi(content));
            }
        if(k==2)
        {
            strcpy(arr[i].title,content);
                //printf("Title is:%s\n",arr[i].title);
                }
            if(k==6)
        {
            fgets(content,300,f);
            arr[i].budget=atoi(content);
            k=-1;
            //      printf("Budget is:%d\n",arr[i].budget);
            i=i+1;
        }
        k++;
    }
   /*
    for(int i=0;i<nrlines;i++)
    {
        list* node=(list*)malloc(sizeof(list));
        node->value=arr[i];
        node->next=NULL;
        if(head==NULL)
        {
            tail=head=node;

        }
        else
        {
            tail=tail->next=node;
        }
        printlist(head);
        //add_ordered(arr[i],head,tail);
    }
    */

    
    return 0;
}

I was only able to store the elements in a listbut not an ordered one. I hope is not too much confusion and i could get some help my code isn t quite the best :(.

George
  • 1
  • 2
  • You need to scan down the list to find the correct insertion point. Walk along till you find an entry 'higher' than the one you want to insert. Then insert the new entry just before it – pm100 May 16 '22 at 13:14

0 Answers0