Re: Why is scanf such a nightmare to use?

Discussion in 'C Programming' started by Malcolm McLean, Sep 2, 2010.

  1. On Sep 2, 12:23 pm, Guillaume Dargaud
    <> wrote:
    > Whenever I try to use scanf I always get into some kind of trouble and
    > revert to strtok or similar finer grain functions.
    >

    The more complicated a function the less flexible it is.

    scanf is fine for reading formatted data with set columns which won't
    be maliciously altered and when you can simply reject the file on a
    parse error. It's no good if the columns are flexible, or even if the
    separators are tabs rather than arbitrary whitespace.
     
    Malcolm McLean, Sep 2, 2010
    #1
    1. Advertising

  2. On Sep 2, 10:31 pm, Malcolm McLean <>
    wrote:
    > The more complicated a function the less flexible it is.


    Good point! But what about functions that become too
    complicated *because* they're trying to be (too) flexible?

    Include me as one who's never mastered the complexities
    of sscanf() and prefers the flexibility of friends
    like strchr().
    .... though until recently I used its BSD synonym, index().
    :: whack in the side of the head ::

    James Dow Allen
     
    James Dow Allen, Sep 4, 2010
    #2
    1. Advertising

  3. On Sep 4, 11:08 am, James Dow Allen <> wrote:
    > On Sep 2, 10:31 pm, Malcolm McLean <>
    >
    > > The more complicated a function the less flexible it is.

    >
    > Good point!  But what about functions that become too
    > complicated *because* they're trying to be (too) flexible?
    >

    The function (or application) ends up as a meta-language.

    Whilst this can be useful, often it's not really worth spending the
    time to master the meta-language.

    It's very difficult to design complex functions for reusablity. 40
    years after computers became common, we still don't really know the
    features that make a function reusable. I'm constantly rewriting code
    that works in the sense that it performs the desired calcualtion and
    is bug free, but is too difficult to integrate into the program I'm
    trying to write.
     
    Malcolm McLean, Sep 4, 2010
    #3
  4. "James Dow Allen" <> wrote in message
    news:...
    On Sep 2, 10:31 pm, Malcolm McLean <>
    wrote:
    > The more complicated a function the less flexible it is.


    <--
    Good point! But what about functions that become too
    complicated *because* they're trying to be (too) flexible?
    -->

    this is called over-engineering...

    flexibility by including every possible feature is a false sense of
    flexibility, and at best means that eventually reaching its limits will
    become catastrophic...

    IMO, it is much better to split a problem into a large number of
    minimalistic pieces, and then rebuild the solution from these pieces, every
    piece with a simple and well defined task.

    with this strategy, new functionality doesn't have to be designed into or
    retrofitted onto the pieces, since if everything works right, then they will
    adapt to the new functionality without noticing that anything has changed.


    granted, there are drawbacks as well (namely, that this process tends to add
    a lot to the total time and effort and produces code larger than idea and
    possibly awkward to drag around), and so often it is better to compromise,
    and implement "short-cut" solutions.

    decomposing a problem too far can also be a case of over-engineering...


    <--
    Include me as one who's never mastered the complexities
    of sscanf() and prefers the flexibility of friends
    like strchr().
    .... though until recently I used its BSD synonym, index().
    :: whack in the side of the head ::
    -->

    I have often wrote special purpose functions operating directly on the
    strings.
    although, part of this was motivated by at one point operating in a
    situation where no standard library was available (OS kernel development),
    and all functionality had to be provided by writing it oneself...

    this ended up becomming a basic part of the pattern...
     
    BGB / cr88192, Sep 6, 2010
    #4
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    923
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,077
    Smokey Grindel
    Dec 2, 2006
  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,452
    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:
    690
    Richard Bos
    May 2, 2006
  5. Nick Keighley

    Re: Why is scanf such a nightmare to use?

    Nick Keighley, Sep 2, 2010, in forum: C Programming
    Replies:
    2
    Views:
    778
    Nobody
    Sep 2, 2010
Loading...

Share This Page