Just another student
Just another student

Reputation: 61

Problem with printing linked lists of structures reading inputs from file csv

I am reading a Csv file with 3 datas for each line. It seems like the list itself fills with datas taken from the Csv file, because if i printf in Insert_list it prints the right things, but still if I print the list it gives me back trash.

I think that the problem is in insert_list because it's the only way to fuck up the list, but i cant find which is. I tried many versions of input in a list and print. What i am actually using seems to be the best choice because atleast if i ask to print Lptr in insert_list, right now, it gives me the right input values. Then if i print the list, it gives me trash.

Structures that I need in my program.

    typedef struct product {
        int prod_code;
        char prod_name[20];
        int price;
        int stockage;
}product;

    typedef struct prod_list {
        product  product;
        struct prod_list *next_ptr;

}prod_list;


typedef prod_list *Prod_ptr;
int threshold=10000; 

Functions:

void insert_list ( Prod_ptr *lptr , int code,  char *name,  int price);
void print_list( Prod_ptr *lptr);

Main:

int main (){
    prod_list *lptr = NULL ;
    FILE *file_ptr;
    file_ptr= fopen( "semplice.csv" , "r");

    if(file_ptr==NULL) {
        printf("error program name");
        return 1;
    }


    int code, price;
    char name[20];

    while (fscanf(file_ptr , "%d,%19[^,],%d", &code , name , &price) == 3) {
       insert_list ( &lptr , code , name , price); 
    }
    print_list(&lptr);


    fclose(file_ptr); 

    return 0;
}




void insert_list(Prod_ptr *lptr, int code , char *name , int price) {



    if(*lptr == NULL){
        Prod_ptr newPtr = malloc(sizeof(prod_list));

        if(newPtr != NULL){
            newPtr->product.prod_code = code;
            strcpy(newPtr->product.prod_name , name);
            newPtr->product.price = price;
            newPtr->product.stockage = rand() % (100001);
            newPtr->next_ptr = NULL;
            *lptr = newPtr;
        }
        else{
            puts(" memoria esaurita");
        }
    }
    else if ((*lptr)->product.prod_code >= code){
        Prod_ptr newPtr = malloc(sizeof(prod_list));

        if ( newPtr != NULL ) {
            newPtr->product.prod_code = code;
            strcpy(newPtr->product.prod_name , name);
            newPtr->product.price = price;
            newPtr->product.stockage = rand() % (100001);
            newPtr->next_ptr = *lptr;
            *lptr = newPtr;


        }
    else{
           puts("mem esaurita");
    }
}
    else{
        insert_list(&((*lptr)->next_ptr) , code , name, price);
    }
}



void print_list( Prod_ptr *lptr){
    Prod_ptr temp = lptr; 
    printf("Input Threshold:");
    while((scanf("%d" , &threshold))!=1 && threshold > 0){
        printf("error input");
        scanf("%*[^\n]%*c");  
    }

    if( temp == NULL){
        puts("Error");
    }
    else {
        for(temp = lptr; temp != NULL; temp = temp->next_ptr){
            if( temp->product.stockage < threshold){
                printf("Product code: %d\nProduct Name: %s\Product price: %d\Stockage: %d\n\n", temp->product.prod_code , temp->product.prod_name , temp->product.price ,  temp->product.stockage );
            }
        }
    }
 }

It compiles but when print_list runs, it gives me this kind of trash: Product code: 11146320 Product Name: Product price: 4199400 Stockage: 0

but if i print the values of lptr inside of insert_list it gives me the right values.

File csv contains:

4;Computer 3;950
12;Computer 4;1050
13;Computer 5;1150
24;Computer 6;1250
25;Computer 7;1350
27;Computer 8;1450
31;Computer 9;1550
32;Computer 10;1650
33;Telefono 1;103
35;Telefono 2;129
38;Telefono 3;155

Upvotes: 0

Views: 70

Answers (2)

wildplasser
wildplasser

Reputation: 44250

The pointer-to-pointer in the insert-function is an excellent way to avoid juggling with excessive variables and conditions. And:to avoid recursion...

So: use it!


void insert_list(struct prodlist **pp, int code , char *name , int price)
{
    struct prodlist *newp ;

        // Advance pp, until it points to the pointer
        // That *should* be pointing at the new entry
        // That pointer *could* be NULL. In that case, we reached the end of the list;
    for(        ; *pp; pp = &(*pp)->next){
        if((*pp)->product.prod_code >= code) break; // Found the place!
        }

    newp = malloc(sizeof *newp);
    if(!newp ){
         fprintf(stderr, "memoria esaurita\n");
         return;
         }

    newp->product.prod_code = code;
    strcpy(newp->product.prod_name , name);
    newp->product.price = price;
    newp->product.stockage = rand() % 100001;
    newp->next = *pp;
    *pp = newp;
}

Upvotes: 1

RobertBaron
RobertBaron

Reputation: 2854

Most of your problems come from using a typedef for a pointer to prod_list. I removed this and used prod_list * directly. This makes things much easier to code and understand. See comments that I inserted in the code.

#include "stdio.h"
#include "stdlib.h"
#include "string.h"

typedef struct product {
    int prod_code;
    char prod_name[20];
    int price;
    int stockage;
} product;

typedef struct prod_list {
    product  product;
    struct prod_list *next_ptr;

} prod_list;

//typedef prod_list *Prod_ptr;

//void insert_list(Prod_ptr *lptr, int code, char *name, int price);
//void print_list(Prod_ptr *lptr);
void insert_list(prod_list **lptr, int code, char *name, int price);
void print_list(prod_list *lptr);

int threshold = 10000;

int main() {
    prod_list *lptr = NULL;
    FILE *file_ptr;
    file_ptr = fopen("C:\\Users\\rbaron\\Documents\\Visual Studio 2015\\Projects\\ConsoleApplication3\\ConsoleApplication3\\Debug\\semplice.csv", "r");

    if (file_ptr == NULL) {
        printf("error program name");
        return 1;
    }

    int code, price;
    char name[20];

    while (fscanf(file_ptr, "%d,%19[^,],%d", &code, name, &price) == 3) {
        //insert_list(&lptr, code, name, price);
        insert_list(&lptr, code, name, price);
    }
    //print_list(&lptr);
    print_list(lptr);
    fclose(file_ptr);

    return 0;
}

//void insert_list(Prod_ptr *lptr, int code, char *name, int price) {
void insert_list(prod_list **lptr, int code, char *name, int price) {

    if (*lptr == NULL) {
        //Prod_ptr newPtr = malloc(sizeof(prod_list));
        prod_list * newPtr = malloc(sizeof(prod_list));

        if (newPtr != NULL) {
            newPtr->product.prod_code = code;
            strcpy(newPtr->product.prod_name, name);
            newPtr->product.price = price;
            newPtr->product.stockage = rand() % (100001);
            newPtr->next_ptr = NULL;
            *lptr = newPtr;
        }
        else {
            puts(" memoria esaurita");
        }
    }
    else if ((*lptr)->product.prod_code >= code) {
        //Prod_ptr newPtr = malloc(sizeof(prod_list));
        prod_list * newPtr = malloc(sizeof(prod_list));

        if (newPtr != NULL) {
            newPtr->product.prod_code = code;
            strcpy(newPtr->product.prod_name, name);
            newPtr->product.price = price;
            newPtr->product.stockage = rand() % (100001);
            newPtr->next_ptr = *lptr;
            *lptr = newPtr;


        }
        else {
            puts("mem esaurita");
        }
    }
    else {
        insert_list(&((*lptr)->next_ptr), code, name, price);
    }
}

//void print_list(Prod_ptr *lptr) {
void print_list(prod_list *lptr) {
    prod_list * temp = lptr;

    // Not sure what this is doing???
    //printf("Input Threshold:");
    //while ((scanf("%d", &threshold)) != 1 && threshold > 0) {
    //    printf("error input");
    //    scanf("%*[^\n]%*c");
    //}

    if (temp == NULL) {
        puts("Error");
    }
    else {
        for (temp = lptr; temp != NULL; temp = temp->next_ptr) {
            if (temp->product.stockage < threshold) {
                //printf("Product code: %d\nProduct Name: %s\Product price: %d\Stockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
                printf("Product code: %d\nProduct Name: %s\nProduct price: %d\nStockage: %d\n\n", temp->product.prod_code, temp->product.prod_name, temp->product.price, temp->product.stockage);
            }
        }
    }
}

Upvotes: 1

Related Questions