Thread: f-scanf ? sscanf ? your help needed.

  1. #1
    Registered User
    Join Date
    Jan 2022
    Posts
    3

    f-scanf ? sscanf ? your help needed.

    i try to read and store into vars in C language each field one file but without success.
    The fields are all separated by ':'.
    Here's my code :
    Code:
        #include <stdio.h>
        #include <string.h>
    
        /*file format
        nom1:PATRICK DUCHMOL:S:2:4:3:30:0
        nom2:HERVE DUCHMOL DE LA POTEE::1:9:6:20:9
        */
        int main()
        {
        FILE * fd  = NULL ;
        char chaine [200];
        char flag [5];
        char nom[50];
        char s[2] ;
        int st1 ;
        int st2 ;
        int st3 ;
        int po ;
        int ti ;
    
        if((fd=fopen("./file.txt", "r"))==NULL) {
        printf("Error\n") ;
        }
    
        while(!feof(fd)) {
    
        if(fscanf(fd, "%s:%[^:]:%s:%d:%d:%d:%d:%d", flag, nom, s, &st1, &st2, &st3, &po, &ti) != 8)
        printf("%s\n", nom) ;
        else
        fprintf(stderr, "Format error.\n") ;
        }
    
        fclose(fd) ;
    
        }
    Thanks for help.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,069
    > if(fscanf(fd, "%s:%[^:]:%s:%d:%d:%d:%d:%d", flag, nom, s, &st1, &st2, &st3, &po, &ti) != 8)
    Maybe try == 8 instead.

    In fact, this is better.
    Code:
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,fd) != NULL ) {
      if ( sscanf(buff, "%s:%[^:]:%s:%d:%d:%d:%d:%d", flag, nom, s, &st1, &st2, &st3, &po, &ti) == 8) {
        // success
      }
    }
    If your input file is screwed up in some way, it's a lot easier to recover if you separate input from parsing.


    > FILE * fd = NULL ;
    It's more traditional to call it fp rather than fd.
    People see 'fd' and think 'file descriptor', which are int's, not FILE* pointers.
    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 2022
    Posts
    3
    Hi, thanks for the suggest, this does not work more

    Code:
     
    #include <stdio.h>
        #include <string.h>
    
        /*file format
        nom1:PATRICK DUCHMOL:S:2:4:3:30:0
        nom2:HERVE DUCHMOL DE LA POTEE::1:9:6:20:9
        */
        int main()
        {
        FILE * fd  = NULL ;
        char chaine [200];
        char flag [5];
        char nom[50];
        char s[2] ;
        int st1 ;
        int st2 ;
        int st3 ;
        int po ;
        int ti ;
    
        if((fd=fopen("./file.txt", "r"))==NULL) {
        printf("Error\n") ;
        	
    }
    
    char buff[BUFSIZ];
    while ( fgets(buff,BUFSIZ,fd) != NULL ) {
    	 printf("%s",buff);
      if ( sscanf(buff, "%s:%[^:]:%s:%d:%d:%d:%d:%d", flag, nom, s, &st1, &st2, &st3, &po, &ti) == 8) {
        printf("%s",nom);
      }
    }
    
        }

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,619
    Perhaps you should try printing out the value sscanf() is returning? It appears to me that your sscanf() is not actually processing the string correctly.

    Look closely at your input lines, they don't have any spaces embedded within them which means that the first "%s" specifier is retrieving the entire string. Remember that the "%s" specifier stops processing at whitespace characters, since your string has no whitespace until the "newline" character the "%s" specifier will retrieve the whole string.

  5. #5
    Registered User
    Join Date
    Jan 2022
    Posts
    3
    I've changed the format of the file and uses sscanf. It works now. Tx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf scanf
    By ddimit02 in forum C Programming
    Replies: 2
    Last Post: 12-20-2010, 03:18 PM
  2. sscanf() help in c
    By ankitsinghal_89 in forum C Programming
    Replies: 2
    Last Post: 01-31-2009, 02:11 PM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. sscanf help!!
    By PunkyBunny300 in forum C Programming
    Replies: 4
    Last Post: 02-27-2003, 02:45 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread