Scanf and wildcard

Discussion in 'C Programming' started by raphfrk@gmail.com, Sep 6, 2008.

  1. Guest

    I have a string that I want to extract some fields out of.

    For example

    ZABDE10FGH20
    ZFDSF11FGG21
    (etc)

    I might want to extract the 2 sets of numbers.

    One option would be to use throwaway buffers.

    char buffer1[6];
    int num1;
    char buffer[4];
    int num2;

    and then use

    fscanf( fp , "%5s%2d%3s%d" , buffer1, num1 , buffer2, num2 );

    Is there a better way to get scanf to skip lines other than having it
    write them to an unused buffer ?
     
    , Sep 6, 2008
    #1
    1. Advertising

  2. Guest

    On Sep 6, 10:36 pm, wrote:

    <snip>

    > Is there a better way to get scanf to skip lines other than having it
    > write them to an unused buffer ?


    you can discard the matched result by using `*'.
    Ie:
    scanf("%*s%s", buf);

    with input:
    hello world

    would read `world' into the buffer.
     
    , Sep 6, 2008
    #2
    1. Advertising

  3. Guest

    On Sep 6, 8:57 pm, wrote:
    > On Sep 6, 10:36 pm, wrote:
    >
    > <snip>
    >
    > > Is there a better way to get scanf to skip lines other than having it
    > > write them to an unused buffer ?

    >
    > you can discard the matched result by using `*'.
    > Ie:
    > scanf("%*s%s", buf);
    >
    > with input:
    > hello world
    >
    > would read `world' into the buffer.


    Thanks
     
    , Sep 6, 2008
    #3
  4. On 2008-09-06, <> wrote:
    > On Sep 6, 10:36 pm, wrote:
    >
    ><snip>
    >
    >> Is there a better way to get scanf to skip lines other than having it
    >> write them to an unused buffer ?

    >
    > you can discard the matched result by using `*'.
    > Ie:
    > scanf("%*s%s", buf);
    >
    > with input:
    > hello world
    >
    > would read `world' into the buffer.


    Yikes! scanf("%s") should not be used without a length qualifier,
    otherwise it is effectively gets().

    --
    Andrew Poelstra
    To email me, use the above email addresss with .com set to .net
     
    Andrew Poelstra, Sep 6, 2008
    #4
  5. Guest

    On Sep 6, 11:35 pm, Andrew Poelstra <> wrote:
    > On 2008-09-06, <> wrote:
    >
    > > On Sep 6, 10:36 pm, wrote:

    >
    > ><snip>

    >
    > >> Is there a better way to get scanf to skip lines other than having it
    > >> write them to an unused buffer ?

    >
    > > you can discard the matched result by using `*'.
    > > Ie:
    > > scanf("%*s%s", buf);

    >
    > > with input:
    > > hello world

    >
    > > would read `world' into the buffer.

    >
    > Yikes! scanf("%s") should not be used without a length qualifier,
    > otherwise it is effectively gets().


    Don't worry. buf is long enough
     
    , Sep 7, 2008
    #5
  6. On Sun, 07 Sep 2008 00:26:06 -0700, vippstar wrote:

    > On Sep 6, 11:35 pm, Andrew Poelstra <> wrote:
    >> On 2008-09-06, <> wrote:
    >>
    >> > On Sep 6, 10:36 pm, wrote:

    >>
    >> ><snip>

    >>
    >> >> Is there a better way to get scanf to skip lines other than having
    >> >> it write them to an unused buffer ?

    >>
    >> > you can discard the matched result by using `*'. Ie:
    >> > scanf("%*s%s", buf);

    >>
    >> > with input:
    >> > hello world

    >>
    >> > would read `world' into the buffer.

    >>
    >> Yikes! scanf("%s") should not be used without a length qualifier,
    >> otherwise it is effectively gets().

    >
    > Don't worry. buf is long enough


    That's just hilarious :) Fancy reading that
    excuse as a comment in some core system.

    --
    OU
     
    Obnoxious User, Sep 7, 2008
    #6
  7. CBFalconer Guest

    wrote:
    > Andrew Poelstra <> wrote:
    >

    .... snip ...
    >
    >> Yikes! scanf("%s") should not be used without a length qualifier,
    >> otherwise it is effectively gets().

    >
    > Don't worry. buf is long enough


    How did you manage to define an infinitely large buf?

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.
     
    CBFalconer, Sep 7, 2008
    #7
    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. SlimFlem
    Replies:
    7
    Views:
    733
    John Timney \( MVP \)
    Dec 22, 2005
  2. Peter Rilling
    Replies:
    1
    Views:
    450
    Elie De Brauwer
    Mar 25, 2005
  3. =?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,409
    those who know me have no need of my name
    Apr 3, 2006
  4. =?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
  5. Replies:
    7
    Views:
    844
Loading...

Share This Page