How to use scanf() for input 2 strings ended of EOF?

Discussion in 'C Programming' started by Alex, Sep 11, 2006.

  1. Alex

    Alex Guest

    The input contains two strings alphanumeric ASCII characters separated
    by whitespace. Input is terminated by EOF.
     
    Alex, Sep 11, 2006
    #1
    1. Advertising

  2. In article <>,
    Alex <> wrote:
    >The input contains two strings alphanumeric ASCII characters separated
    >by whitespace. Input is terminated by EOF.


    How nice for it.
     
    Kenny McCormack, Sep 11, 2006
    #2
    1. Advertising

  3. Alex wrote:

    > The input contains two strings alphanumeric ASCII characters separated
    > by whitespace. Input is terminated by EOF.


    maybe you want to look at fgets() and sscanf()?

    --
    Nick Keighley
     
    Nick Keighley, Sep 11, 2006
    #3
  4. Alex

    Chris Dollin Guest

    Alex wrote:

    > The input contains two strings alphanumeric ASCII characters separated
    > by whitespace. Input is terminated by EOF.


    (If EOF didn't terminate it, it wouldn't be called *E*OF; it seems a bit
    redundant to say that.)

    So the input is required to have no line-end characters? That seems ...
    unwise.

    --
    Chris "remember Indiana 3" Dollin
    "I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/
     
    Chris Dollin, Sep 11, 2006
    #4
  5. Alex

    Richard Bos Guest

    Chris Dollin <> wrote:

    > Alex wrote:
    >
    > > The input contains two strings alphanumeric ASCII characters separated
    > > by whitespace. Input is terminated by EOF.

    >
    > (If EOF didn't terminate it, it wouldn't be called *E*OF; it seems a bit
    > redundant to say that.)
    >
    > So the input is required to have no line-end characters?


    Where do you read that?

    Richard
     
    Richard Bos, Sep 11, 2006
    #5
  6. Alex

    Chris Dollin Guest

    Richard Bos wrote:

    > Chris Dollin <> wrote:
    >
    >> Alex wrote:
    >>
    >> > The input contains two strings alphanumeric ASCII characters separated
    >> > by whitespace. Input is terminated by EOF.

    >>
    >> (If EOF didn't terminate it, it wouldn't be called *E*OF; it seems a bit
    >> redundant to say that.)
    >>
    >> So the input is required to have no line-end characters?

    >
    > Where do you read that?


    Because the spec doesn't appear to allow it:

    >>> The input contains two strings alphanumeric ASCII characters
    >>> separated by whitespace. Input is terminated by EOF."


    There's nothing in there that allows whitespace (of which end-of-line,
    aka \n, is an instance) after the second alphanumeric string.

    It's probably a buggy specification, although some people might take
    the position that it's a picky reading.

    --
    Chris "pleasing coincidence" Dollin
    Nit-picking is best done among friends.
     
    Chris Dollin, Sep 11, 2006
    #6
  7. Alex

    Chris Dollin Guest

    Chris Dollin wrote:

    > Richard Bos wrote:
    >
    >> Chris Dollin <> wrote:
    >>
    >>> Alex wrote:
    >>>
    >>> > The input contains two strings alphanumeric ASCII characters separated
    >>> > by whitespace. Input is terminated by EOF.
    >>>
    >>> (If EOF didn't terminate it, it wouldn't be called *E*OF; it seems a bit
    >>> redundant to say that.)
    >>>
    >>> So the input is required to have no line-end characters?

    >>
    >> Where do you read that?

    >
    > Because the spec doesn't appear to allow it:
    >
    >>>> The input contains two strings alphanumeric ASCII characters
    >>>> separated by whitespace. Input is terminated by EOF."

    >
    > There's nothing in there that allows whitespace (of which end-of-line,
    > aka \n, is an instance) after the second alphanumeric string.
    >
    > It's probably a buggy specification, although some people might take
    > the position that it's a picky reading.


    .... and I see that of course it allows end-of-lines /between/ the
    tokens. Just not at the end.

    A sloppy picky reading, then.

    Blame it on the latte's confounded absence.

    --
    Chris "at least it's nearly tea-time" Dollin
    The shortcuts are all full of people using them.
     
    Chris Dollin, Sep 11, 2006
    #7
  8. Alex wrote:

    > The input contains two strings alphanumeric ASCII characters separated
    > by whitespace. Input is terminated by EOF.


    man sscanf or google sscanf

    double a, b;
    if( 2 != sscanf(input, "%lf %lf", &a, &b) ) printf("error\n");

    Konstantin
     
    Konstantin Miller, Sep 11, 2006
    #8
  9. Alex

    kondal Guest

    Alex wrote:
    > The input contains two strings alphanumeric ASCII characters separated
    > by whitespace. Input is terminated by EOF.


    char buf[1000]="";
    int len = 1000; /*length of the strings combined. */
    fread(buf, len, 1, stdin);

    Then use strtok to get the individual strings.

    Hope this works!
     
    kondal, Sep 11, 2006
    #9
  10. Konstantin Miller wrote:
    > Alex wrote:
    >
    > > The input contains two strings alphanumeric ASCII characters separated
    > > by whitespace. Input is terminated by EOF.

    >
    > man sscanf or google sscanf
    >
    > double a, b;
    > if( 2 != sscanf(input, "%lf %lf", &a, &b) ) printf("error\n");


    *strings*, not floating point numbers.

    Robert Gamble
     
    Robert Gamble, Sep 11, 2006
    #10
  11. Alex

    kondal Guest

    kondal wrote:
    > Alex wrote:
    > > The input contains two strings alphanumeric ASCII characters separated
    > > by whitespace. Input is terminated by EOF.

    >
    > char buf[1000]="";
    > int len = 1000; /*length of the strings combined. */
    > fread(buf, len, 1, stdin);
    >
    > Then use strtok to get the individual strings.
    >
    > Hope this works!


    if the length of the strings isn't known, you can read and fill the
    buffer till eof is reached using feof function.
     
    kondal, Sep 12, 2006
    #11
  12. "kondal" <> writes:
    > kondal wrote:
    >> Alex wrote:
    >> > The input contains two strings alphanumeric ASCII characters separated
    >> > by whitespace. Input is terminated by EOF.

    >>
    >> char buf[1000]="";
    >> int len = 1000; /*length of the strings combined. */
    >> fread(buf, len, 1, stdin);
    >>
    >> Then use strtok to get the individual strings.
    >>
    >> Hope this works!

    >
    > if the length of the strings isn't known, you can read and fill the
    > buffer till eof is reached using feof function.


    feof() probably isn't what you want. fread() returns the number of
    items succesfully read; if its return value is less than the number of
    items requested, you can *then* use feof() to find out whether it was
    because you reached the end of the file or because of an error.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 12, 2006
    #12
    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. hugo27

    Does scanf ever return EOF?

    hugo27, Jun 18, 2004, in forum: C Programming
    Replies:
    7
    Views:
    15,640
    Dan Pop
    Jun 21, 2004
  2. Kobu
    Replies:
    10
    Views:
    627
    Keith Thompson
    Mar 4, 2005
  3. Replies:
    4
    Views:
    671
    Walter Roberson
    Sep 9, 2005
  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,399
    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:
    679
    Richard Bos
    May 2, 2006
Loading...

Share This Page