Thread: problem with sscanf

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    problem with sscanf

    Hi,

    I'm doing a program that read a file, then put the first line in a string. Here is a part of my file
    Code:
    TXSYN 0.0078 CIE qqq qqq qqq
    as I said, the line is put in char line. I want sIdentif to contain the string HRSUP, fRegle to contain the float 40.0000 (this is the only one that work), sType to contain the string CIE and sDescript to contain qqq qqq qqq.

    Code:
    char sIdentif[6];  
    float fRegle;   
    char sType[4];     
    char sDescript[50];  
    char line[BUFSIZ];
            
             sscanf (line, "%s %s %d %s",sIdentif,&fRegle,sType,sDescript);
             printf ("%s %s %d %s",sIdentif,&fRegle,sType,sDescript);
    when I ask him to display char line, he do the job fine (which mean the reading is alright, but when I do the printf after my sscanf, he show stuff like
    Code:
    000 0.0078 2293032
    as you can see, the only one working is fRegle.
    If someone can help me do this right, it would greatly be appreciated.

    thanks

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    First of all, sscanf() parses a string. That string must be initialized. You have to set it to something, possibly with fgets().
    Code:
    sscanf (line, "%s %s %d %s",sIdentif,&fRegle,sType,sDescript);
    You have %s for &fRegle, which is a float, and should be %f; you have %d for sType, which is a char*, and should be %s. Only sIdentif and sDescript have the right format specifiers. Ditto for the prints.

    [edit] And when you print a float, don't take the address of it with &a_float. This works:
    Code:
    printf("%f\n", a_float);
    [/edit]
    Last edited by dwks; 04-14-2007 at 01:57 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Using %s to read into a float?
    Using %d to read into a char array?

    Dunno, back to school on the basics me thinks.

    Or discover the delights of
    gcc -W -Wall -ansi -pedantic
    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
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    I can send you my complete program if you want, but as I said, my string line already have TXSYN 0.0078 CIE qqq qqq qqq, I want to extract this content into 4 var.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Did you read my post and Salem's? You're trying to read a float with %s instead of %f, and a char array with %d instead of %s.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Ok, I did what you told me, here are the modifications
    Code:
             sscanf (ligne, "%s %f %s %s",sIdentif,&fRegle,sType,sDescript);
             printf ("%s %f %s %s",sIdentif,&fRegle,sType,sDescript);
    now, it print
    Code:
    TXSYN 0.000000 CIE qqq qqq
    The float isn't right and when he read the qqq qqq qqq, he only read one qqq and double it to make qqq qqq.

    by the way, I tried without the adress but when the program execute, he crash, so could you be a bit more precise please, I'm pretty new in this.
    Last edited by nevrax; 04-14-2007 at 02:16 PM.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Right, another point from my original post.
    And when you print a float, don't take the address of it with &a_float.
    Use fRegle in the prinf, not &fRegle, and only in the printf. You need to & in for the scanf.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Ok, I get it (I had remove the 2 &)now everything work except that he stop reading the qqq qqq qqq after the space, so he only display qqq ( as expected), how can I fix this.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You mean, you want one string to contain spaces as well?
    Code:
    scanf("%[^ ]", string);
    instead of %s. (I think. Salem will know.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    It doesn't really work. I tried with [^s]" which work with qqq qqq qqq, but it dosn't work with sentences like "I'm playing rubic cube", he only show stuff like the 2 or 3 first word or the 2 first with half of the third. This is not due to sDescript array since his length is [1000]

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    It's "%[^ ]"; no s on the end. I know, it looks weird; but what about scanf() isn't?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    If you're speaking about
    Code:
             sscanf (ligne, "%s %f %s %[^ ]",sIdentif,&fRegle,sType,sDescript);
             printf ("%s %f %s %s",sIdentif,fRegle,sType,sDescript);
    it dosen't work, he still stop reading after a space. I tried it first, then I tried with a s and it worked with the qqq qqq qqq, but not for sentences.

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Hmm . . . well, since I don't know the format specifier, you could use something like this:
    Code:
    int pos;
    sscanf (line, "%s %f %s %p",sIdentif,&fRegle,sType,&n);
    strcpy(sDescript, line+pos);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Thanks for your help in all this dwks. For my personal culture I'll post a new thread and if no one know, I'll take your idea.

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Also,
    Code:
    #include <stdio.h>
    
    int main()
    {
       static const char filename[] = "file.txt";
       FILE *file = fopen(filename, "r");
       if ( file )
       {
          char sIdentif[6];  
          float fRegle;   
          char sType[4];     
          char sDescript[50];  
          char line[BUFSIZ];
          while ( fgets(line, sizeof line, file) )
          {
             if ( sscanf (line, "%5s %f %3s %49[^\n]", 
                          sIdentif, &fRegle, sType, sDescript) == 4 )
             {
                printf ("sIdentif  = \"%s\"\n", sIdentif);
                printf ("fRegle    = %g\n",     fRegle);
                printf ("sType     = \"%s\"\n", sType);
                printf ("sDescript = \"%s\"\n", sDescript);
             }
          }
       }
       else
       {
          perror(filename);
       }
       return 0;
    } 
    
    /* my output
    sIdentif  = "TXSYN"
    fRegle    = 0.0078
    sType     = "CIE"
    sDescript = "qqq qqq qqq"
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. sscanf problem
    By LordShme in forum C Programming
    Replies: 5
    Last Post: 12-05-2006, 09:09 PM
  3. Weird problem with sscanf
    By g4j31a5 in forum C++ Programming
    Replies: 17
    Last Post: 10-04-2006, 09:16 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM