Thread: sscanf query

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    7

    sscanf query

    if I have a string say 12345.67123456 and I want to break it into 2 seperate character arrays of 12345.67 and 123456 how should I go about it I was trying:


    sscanf( str, "%9s%7s", first, second );

    but this gives me

    12345.6712345
    and
    123456

    please could you let me know where I am going wrong.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Try %9.9s%7s

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    Cheers will do.
    But could you just please explain why as I don't just want to make it work I want to understand why for the future.

    Thanks

    Taz

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    I have just tried this and it gives me nonesense characters.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    329
    Just a suggestion.
    When printing characters, %9.3s will print a string with maximum 9 characters, but minimum 3 characters.
    I set different numbers just to separate their meaning

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [edit]Wrong answer previously, as noted by your reply.

    Adding this one in its place.
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       const char text[] = "12345.67123456";
       char first [ 9 ], second [ 7 ];
       if ( sscanf(text, "%8s%6s", first, second) == 2 )
       {
          printf("first = \"%s\", second = \"%s\"\n", first, second);
       }
       return 0;
    }
    
    /* my output
    first = "12345.67", second = "123456"
    */
    It seems to me the same as your original except the added context.
    Last edited by Dave_Sinkula; 08-31-2004 at 12:32 PM. Reason: need to read better - replaced wrong answer with this
    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.*

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    That isn't what I am trying to do I want 123456.78 to be first and 123456 to be second

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Leonix
    That isn't what I am trying to do I want 123456.78 to be first and 123456 to be second
    You don't even have an 8 in your string...
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    oh yes well I will probably struggle with that one then. well spotted.
    what I meant was :
    first = "12345.67"
    second = "123456"

    as 2 seperate strings.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    ok this is the function in question

    Code:
    void validateC( char *tempstring )
    {
    	int loop;
    	char CreditLimit[9], CustomerBalance[7];
    	char *string_ptr = &tempstring[6];
    	printf("\n\nValidation of C ");
    	sscanf( tempstring, "%c%5s", &Record.Cre.RecordType, &Record.Cre.CustomerCode);
    	for( loop = 0; loop < 20; loop++ )
    	{
    		Record.Cre.CustomerName[loop] = *string_ptr++;
    	}
    	for( loop = 0; loop < 60; loop++ )
    	{
    		Record.Cre.CustomerAddress[loop] = *string_ptr++;
    	}
    	sscanf( string_ptr, "%9s%7s", &CustomerBalance, &CreditLimit );
    	printf("\n%c\n%5s\n%20s\n%60s\n%9s\n%7s", Record.Cre.RecordType, Record.Cre.CustomerCode,
    									  Record.Cre.CustomerName, Record.Cre.CustomerAddress,
    									  CustomerBalance, CreditLimit);
    }
    where the tempstring passed to it is :
    c12345andrew Anderson 100 saint Road; Lower heaton; Blackpool; Bp3 4JZ 123456.780010000

    but I get
    123456.780010000 for my CustomerBalance
    and then
    0010000 for my CreditLimit

    I have tried to work it out myself as i don't expect you guys to do the work for me but I don't know where I am going wrong.

    by the wat the reason I first set it to a string is because it could also hold letters. ie "unknown" could be sent in this field.

    Thanx in advance

    Taz

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are we just supposed to guess what Record contains? How about Record.Cre? That too? We can't very well debug your code if you insist on giving incomplete information.

    Also, why don't you just use sscanf properly, and scrap your array-filling-loops all together?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Well, if you want to do this with sscanf(), using strings, you pretty much have to know two things:

    1. How sscanf() works.
    2. How the input data are arranged in the input string.

    You can teach yourself a lot of things by using printf() right after each application of sscanf():

    Code:
    sscanf( tempstring, "%c%5s", &Record.Cre.RecordType, &Record.Cre.CustomerCode);
    printf(""\n\nRecordType = %d, CustomerCode: <%s>\n\n", 
    Record.Cre.RecordType,Record.Cre.RecordType);
    Do it after each sscanf(), to see what' really happening.

    Oh, by the way, consider this

    Code:
    	for( loop = 0; loop < 20; loop++ )
    	{
    		Record.Cre.CustomerName[loop] = *string_ptr++;
    	}
    What do you get with
    Code:
      printf("CustomerName: <%s>\n", Record.Cre.CustomerName);
    Unless the array was initialized to all zero, you should put
    Code:
    Record.Cre.CustomerName[20] = '\0';
    Even if it had been initialized to 0, I would always put it here, in case someone decides to change the initialization at some later time.

    Does any of this give you anything more to work with? Wouldn't you really rather do it yourself? This stuff is kind of fun when it works.

    Dave
    Last edited by Dave Evans; 08-31-2004 at 05:34 PM.

  13. #13
    Registered User
    Join Date
    Aug 2004
    Posts
    7
    I didn't include what Record and Record.cre hold as they work fine I just want to know where I am going wrong with regard to CustomerBalance and Creditlimit. which isn't part of the Record structure

    sscanf( string_ptr, "%9s%7s", &CustomerBalance, &CreditLimit );

    when I print the other fields they all show what they should. so my question only relates to the sscanf() line really.

    Hope this helps you help me.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    I think you're much better off using say strncpy() to copy specific bits of the string which can be then passed to simpler sscanf() calls, rather than trying to do it all in one massively complex sscanf call.

    Eg
    Code:
    char temp[10];
    strncpy( temp, buff, 9 ); temp[9] = '\0';
    sscanf( temp, "%f", &myfloat );
    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.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Leonix
    I didn't include what Record and Record.cre hold as they work fine I just want to know where I am going wrong with regard to CustomerBalance and Creditlimit. which isn't part of the Record structure

    sscanf( string_ptr, "%9s%7s", &CustomerBalance, &CreditLimit );

    when I print the other fields they all show what they should. so my question only relates to the sscanf() line really.

    Hope this helps you help me.
    Actually, it doesn't. It's as I stated in my reply: Just because you know "it works right", doesn't mean I know it does. You say you're having problems with sscanf "not working", and yet, you include sscanf calls that I can only guess at.

    How am I supposed to know if those calls are right? The fact is, I can't. Not with the information provided. Therefore, it's rather hard for me to debug your program, since you aren't providing enough information.

    Quzah.
    Hope is the first step on the road to disappointment.

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. query problem
    By arian in forum C# Programming
    Replies: 1
    Last Post: 08-18-2008, 01:49 PM
  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()
    By task in forum C Programming
    Replies: 4
    Last Post: 11-22-2003, 04:43 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM