Thread: sscanf

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    sscanf

    Hi,

    Here is my input from a file:

    ------------------------------------
    Other Names=Andrew
    -----------------------------------

    i want to divide the string into 2 parts"

    header = "Other Names"
    content= "Andrew"

    Here is my code:

    <code>
    while ((fgets(line, sizeof(line), fp)) != NULL)
    {
    sscanf(line,"%s=%s\n", header, content);
    printf("%s\n", header);
    }
    </code>


    However, the output is:

    header = "Other"
    content = some rubbish


    What could be the problem?
    Thanks

  2. #2
    Registered User MisterBadger's Avatar
    Join Date
    Mar 2002
    Posts
    16
    Hello cheryl_li
    Code:
    #include<stdio.h>
    
    int main(void)
    {
     char *line="Other Names=Andrew", header[20], content[20];
    
     sscanf(line,"%11c=%s\n", header, content);
    
     header[11]='\0';
    
     printf("HEADER : %s\nCONTENT: %s\n\nEnd of Program!", header, content);
    
    return 0;
    }
    In your code, the first string was copied into "header" up to the space following "Other".

    The above code appears to work because the first conversion made by sscanf reads 11 characters into "header".

    Does this help you?

    Cheers MrB

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    Yeah
    got it, thanks man! Both of you

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 question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM