sscanf()

This is a discussion on sscanf() within the C Programming forums, part of the General Programming Boards category; with sscanf() if I want to pick up strings do I allow for the '\n' i.e. if i want to ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    66

    sscanf()

    with sscanf() if I want to pick up strings do I allow for the '\n'

    i.e. if i want to pick up 2 strings of 20 characters & 15characters

    would it be sscanf( string, "%20s%15s", string1, string2)

    or

    sscanf( string, "%21s%16s", string1, string2 )

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    Well %s will ignore / skip over any newline characters

    If you have
    char string1[20];

    Then the max field width for conversion would be
    "%19s"

    Making the conversion size automatically dependent on the buffer size is more difficult than it ought to be.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    It is:

    char string1[21]; /* 20 characters plus newline character */

    I have allowed for the newline character when I created it.

    what I was unsure of is to pick them out of a temporarty string would it be:

    sscanf( temp_string, "%20s", string1 );

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,558
    > char string1[21]; /* 20 characters plus newline character */
    No, 20 chars + a nul (\0)

    Like I just said, "%s" will NOT copy a \n character even if one is present.

    If you want to read a string including a \n, then use fgets()
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    sorry I am just being stupid now. I didn't mean newline I ment null

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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21