scanf help

Discussion in 'C Programming' started by Stu, Oct 11, 2006.

  1. Stu

    Stu Guest

    Can somebody give me a scanf statment that will parse out the value
    between the pipe signs but not including the the pipe signs.


    @xxx:|this value|

    Thanks to all that answer this post.
     
    Stu, Oct 11, 2006
    #1
    1. Advertising

  2. Stu

    jacob navia Guest

    Stu wrote:
    > Can somebody give me a scanf statment that will parse out the value
    > between the pipe signs but not including the the pipe signs.
    >
    >
    > @xxx:|this value|
    >
    > Thanks to all that answer this post.
    >

    Why scanf?

    Just read the line in a buffer, then:


    char *start = strchr(line,'|'); // find the first |
    char *end = strrchr(line,'|'); // find the last |
    if (start)
    start++; // start points to the start of the data
    else {
    // treat error
    }
    if (end) {
    // Zero terminate the data
    *end = 0;
    }
    else {
    // reat error
    }
    strcpy(destination_buffer,start);

    This will parse
    @xxx;| this value with embedded | signs that are ignored|
    correctly, unlike scanf. Besides the code is much simpler.
    Scanf is kind of too fragile to be used in serious contexts.

    (My personal opinion)

    jacob
     
    jacob navia, Oct 11, 2006
    #2
    1. Advertising

  3. Stu

    Bill Reid Guest

    Stu <> wrote in message
    news:...
    > Can somebody give me a scanf statment that will parse out the value
    > between the pipe signs but not including the the pipe signs.
    >
    >
    > @xxx:|this value|
    >
    > Thanks to all that answer this post.
    >

    Uh, aside from the usual advice not to use scanf (too dangerous),
    and assuming you're trying to get the strings between the "pipes",
    use this type of expression:

    "%[^|]|%[^|]|"

    The "^" inside the brackets indicates the characters NOT to include
    in the string...

    ---
    William Ernest Reid
     
    Bill Reid, Oct 13, 2006
    #3
  4. Stu

    CBFalconer Guest

    Bill Reid wrote:
    > Stu <> wrote in message
    >
    >> Can somebody give me a scanf statment that will parse out the value
    >> between the pipe signs but not including the the pipe signs.
    >>
    >> @xxx:|this value|

    >
    > Uh, aside from the usual advice not to use scanf (too dangerous),
    > and assuming you're trying to get the strings between the "pipes",
    > use this type of expression:
    >
    > "%[^|]|%[^|]|"
    >
    > The "^" inside the brackets indicates the characters NOT to include
    > in the string...


    For the efficiency buffs, remember that regular expression use
    almost always involves considerable CPU time. A simple routing
    such as toksplit is much more efficient.

    --
    Some informative links:
    <news:news.announce.newusers
    <http://www.geocities.com/nnqweb/>
    <http://www.catb.org/~esr/faqs/smart-questions.html>
    <http://www.caliburn.nl/topposting.html>
    <http://www.netmeister.org/news/learn2quote.html>
    <http://cfaj.freeshell.org/google/>
     
    CBFalconer, Oct 13, 2006
    #4
  5. In article <>,
    CBFalconer <> wrote:

    >> "%[^|]|%[^|]|"


    >For the efficiency buffs, remember that regular expression use
    >almost always involves considerable CPU time.


    scanf()'s %[^...] notation is not a regular expression, but something
    much simpler.

    More generally, regular expressions can be compiled into very
    time-efficient finite state machines, though certain complicated ones
    require impractically large amounts of space.

    -- Richard
     
    Richard Tobin, Oct 13, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramprasad A Padmanabhan

    scanf help again

    Ramprasad A Padmanabhan, Jul 24, 2003, in forum: C Programming
    Replies:
    3
    Views:
    429
    Dave Thompson
    Aug 8, 2003
  2. Rob

    help with infinite loops and scanf

    Rob, Jul 26, 2003, in forum: C Programming
    Replies:
    8
    Views:
    775
    Peter Shaggy Haywood
    Jul 29, 2003
  3. Sivarn

    Help with scanf

    Sivarn, Apr 1, 2004, in forum: C Programming
    Replies:
    23
    Views:
    1,286
    Dan Pop
    Apr 13, 2004
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,410
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    684
    Richard Bos
    May 2, 2006
Loading...

Share This Page