Thread: Function SSCANF problem!

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    5

    Function SSCANF problem!

    Hi all, my code seems to work, the only problem is that the sscanf function makes me a double copy of a value. I'm sure the problem is the sscanf function because I tried to print the vector filled by the fgets function and print the file correctly.



    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    #define N 100
    
    
    struct INFORMAZIONE{
    int eta_informazione;
    int prezzo_informazione;
    struct INFORMAZIONE *next;
    };
    
    
    typedef struct INFORMAZIONE informazione;
    
    
    
    
    
    
    
    
    
    
    void Genera_Lista(FILE *ptr_paganti);
    
    
    int main (){
    
    
    FILE *ptr_paganti;
    ptr_paganti = fopen("Utenti_PAGANTI.txt" , "r");
    Genera_Lista(ptr_paganti);
    
    
    return 0;
    }
    
    
    void Genera_Lista(FILE *ptr_paganti){
    
    
    int somma = 0;
    int eta_media = 0;
    int num_visitatori = 0;
    char utenti_line[N];
    informazione *head, *current;
    head = current = NULL;
    
    
    
    
    
    
    if (ptr_paganti == NULL){
        printf("Errore, file non trovato!");
        }
    else {
     while(fgets(utenti_line,sizeof(utenti_line),ptr_paganti)!= NULL){
        char cognome_temp[N];
         char nome_temp[N];
         int eta_temp;
         int biglietto_temp;
     printf("%s\n" , utenti_line);
    
    
     sscanf(utenti_line, "%s %s %d %d", cognome_temp, nome_temp, &eta_temp, &biglietto_temp);
     informazione *node = malloc(sizeof(informazione));
     node-> eta_informazione = eta_temp;
     node-> prezzo_informazione = biglietto_temp;
     node->next = NULL;
    
    
        if(head == NULL){
                    current = head = node;
                }
        else {
                    current = current->next = node;
                }
        num_visitatori = num_visitatori +1;
            }
            fclose(ptr_paganti);
    
    
        }
     
         printf("\n\n");
         printf("\nLINKED LIST\n");
     
     for(current = head; current ; current=current->next){
        printf("(%d," , current->eta_informazione);
        printf("%d)-> " , current->prezzo_informazione);
        somma = somma + current->eta_informazione;
        eta_media = somma/num_visitatori;
    }
    
    
    printf("NULL\n");
    printf("\n\n");
    printf("Il numero di visitatori del cinema e' : %d\n" , num_visitatori);
    printf("L'eta' media dei clienti e' : %d" , eta_media); 
     }









    This is the output and how you can see in the second node there is a copy of the first.


    Function SSCANF problem!-aaaa-png

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Well you could check the return result of sscanf to make sure you assigned the integers correctly.

    Also, you need to work on your indentation.
    Indentation style - Wikipedia
    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
    Jan 2018
    Posts
    5
    Quote Originally Posted by Salem View Post
    Well you could check the return result of sscanf to make sure you assigned the integers correctly.

    Also, you need to work on your indentation.
    Indentation style - Wikipedia

    I tried to use a print to check sscanf but how i said it doesn't assign correctly the integers only for the second iteration, the first, third and fourth assignment are correct .


    PS:Thanks for the adivce

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    So perhaps the 2nd line isn't all that it seems.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another example of sscanf function
    By lsatenstein in forum C Programming
    Replies: 2
    Last Post: 01-26-2016, 02:22 PM
  2. understanding sscanf() function
    By ak47 in forum C Programming
    Replies: 6
    Last Post: 01-03-2014, 11:50 PM
  3. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  4. problem with sscanf
    By nevrax in forum C Programming
    Replies: 19
    Last Post: 04-14-2007, 10:59 PM
  5. What is wrong with my sscanf function?
    By doampofo in forum C Programming
    Replies: 5
    Last Post: 03-09-2003, 10:42 PM

Tags for this Thread