Thread: sscanf() examples?

  1. #1
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335

    sscanf() examples?

    I've tried reading around and i haven't had any luck getting sscanf to work with what i'm trying to do. Initially, i was using strtok to chop up some values that i need to store them into seperate variables in my link list. I was wondering if there are any simple examples for sscanf
    to save the following somewhere so i can store them into a linklist:

    Sarah 111.2

    I just want to be able to get a reference to "Sarah" and store it somewhere so i can pass it to my addtolist method. It's quiet easy hard coding it i.e. having a variable to store the name and total earnings but once i add more employees to the text file the old values are overwritten.

    Any help would be appreciated.

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, here is an exmaple of sccanf fucntion
    Code:
    #include<stdio.h>
    
    int main()
    {
        char buff[20]={"Sarah 111.2"};
        char name[10];
        float num;
        
        sscanf(buff,"%s %f",name,&num);
        
        printf("%s %.2f",name,num);
        
        getchar();
    }
    /*myouput
    Sarah 111.20
    */
    hope this hepls
    ssharish2005

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    char name[BUFSIZ];
    double val;
    char buff[BUFSIZ] = "Sarah 111.2";   /* replace with calls to fgets() to read from a file/user */
    if ( sscanf (buff, "%s %lf", name, &val ) == 2 ) {
      /* valid data, do stuff */
    }
    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.

  4. #4
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    Thanks alot although theres a minor problem. Both solutions work fine for static text. I've basically done the same the same thing, but created a buffer dynamically depending on the text file inputted. if the text contains 'Sarah 111.2' it works fine. But if i add another line with a different name the second line is not printed

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    well, you have to read line by line using a fgets function and use sscanf to get name and the number. the follwing code might help u

    Code:
    #include<stdio.h>
    
    int main()
    {
        FILE *fp;
        char name[25];
        char buff[BUFSIZ];
        float num;
        
        if((fp=fopen("test.txt","r"))==NULL)  
        {
            perror("File cannot be opened");
            getchar();
            exit(1);
        }
        
        while(fgets(buff,sizeof(buff),fp)!=NULL)  // read a single line from the file till i get EOF
        {
            if(sscanf(buff,"%s %f",name,&num)==2)  // getting the name and the number from the  buffer which was read from the file
            printf("%s %.1f\n",name,num);
            else
            printf("line cannot be printed\n");
        }
        
        getchar();
        return 0;
    }
    /* myoutput
    Sarah 111.2
    John 222.3
    Alan 333.4
    */
    and the test file test.txt
    Sarah 111.2
    John 222.3
    Alan 333.4

    ssharish2005

  6. #6
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    ssharish, thanks alot. one problem though if i put sarah 11 in the text file it converts it to a double(11.0) is there anyway to get it to go to the "else" statement (ive tested with string etc it works fine, puts out an error as expected)

  7. #7
    Learner Axel's Avatar
    Join Date
    Aug 2005
    Posts
    335
    never mind, i figured it out. But there's still one problem:

    Don 33.65 11.95
    Sarah 55
    Tom 122 34

    since the file doesn't have correct float variables (line 2, 3) is there anyway to send an error even though the first line is valid? At the moment as soon as it finds one it will print it and then the else statement comes into effect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM