Very very very basic question

Discussion in 'C Programming' started by Peter, Feb 8, 2005.

  1. Peter

    Peter Guest

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main(void) {

    char input_string[50];

    printf("Please enter conversion: ");
    scanf("%s", input_string);

    printf("The output is %s\n", input_string);

    exit(0);
    }


    Why does this code ignore any input text after a space?

    e.g. If I enter "Hello World" it only stores (and prints) "Hello" in
    input_string.

    Sorry for the stupid question, it's my first C program. :)
     
    Peter, Feb 8, 2005
    #1
    1. Advertising

  2. Peter

    Peter Guest

    "those who know me have no need of my name" <>
    wrote in message news:...
    > in comp.lang.c i read:
    >
    >> scanf("%s", input_string);

    >
    >>Why does this code ignore any input text after a space?

    >
    > because that's what it's supposed to do:
    >
    > s Matches a sequence of bytes that are not white-space characters.


    Thanks.

    What's the function I'm looking for then to take in any length of string
    (whether white space or not)?
     
    Peter, Feb 8, 2005
    #2
    1. Advertising

  3. in comp.lang.c i read:

    > scanf("%s", input_string);


    >Why does this code ignore any input text after a space?


    because that's what it's supposed to do:

    s Matches a sequence of bytes that are not white-space characters.

    --
    a signature
     
    those who know me have no need of my name, Feb 8, 2005
    #3
  4. Peter

    John Valko Guest

    Peter wrote:

    > char input_string[50];
    >
    > printf("Please enter conversion: ");
    > scanf("%s", input_string);


    > Why does this code ignore any input text after a space?
    >
    > e.g. If I enter "Hello World" it only stores (and prints) "Hello" in
    > input_string.
    >


    Using %s in scanf() will read characters up until it hits any whitespace
    character. If you want to read a whole line including spaces, use fgets():

    fgets(input_string, sizeof input_string, stdin);

    Mind that fgets() will leave the \n on the end, assuming there is one.

    --John
     
    John Valko, Feb 8, 2005
    #4
  5. Peter

    Dmitry Guest

    Using GNU libc, 'getline()' is a best choice (IMHO).

    Another way is 'scanf' with the next pattern:

    scanf("%[^\n\r]", input_string);

    i.e "everything before newline or carriage return symbol". Beware of
    buffer overflow ;)

    Peter wrote:
    > "those who know me have no need of my name" <>
    > wrote in message news:...
    >
    >>in comp.lang.c i read:
    >>
    >>
    >>>scanf("%s", input_string);

    >
    > What's the function I'm looking for then to take in any length of string
    > (whether white space or not)?
    >
    >
     
    Dmitry, Feb 8, 2005
    #5
  6. Peter

    -berlin.de Guest

    Peter <> wrote:
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include <string.h>


    You don't seem to need the last one here.

    > int main(void) {
    > char input_string[50];
    > printf("Please enter conversion: ");


    You need here additionally

    fflush( stdout );

    to make sure that that string is printed out immediately. Strings
    that don't end in a '\n' can stay in the internal output buffers
    of the printf() function and only the fflush() makes sure it gets
    written to the screen in this case.

    > scanf("%s", input_string);
    > printf("The output is %s\n", input_string);
    > exit(0);


    A "return 0;" or "return EXIT_SUCCESS;" will do here perfectly well,
    no reason to kill the program;-)

    > }


    > Why does this code ignore any input text after a space?


    Because scanf() always stops at spaces when reading in a string
    (unless you tell it otherwise), assuming that that's the end of
    the input string. The way to tell scanf() not to stop at spaces
    is using

    scanf("%[^\n]", input_string);

    The "%[^\n]" tells scanf() only to stop on a newline character. But
    than you still have a potential problem: if the user enters more
    than 49 characters scanf() will happily write them past the end of
    your 'input_string' array and then in principle everything can
    happen (it may even seem to work). So you better make that

    scanf("%49[^\n]", input_string);

    to tell scanf() to accept not more than the 49 characters fitting
    into the buffer (don't forget about the trailing '\0' that's needed
    at the end of the string).

    Since you seem to want to read a simple line it should be a lot
    simpler to use fgets() here instead of scanf() (and never, ever
    use gets(), it's horribly broken!). scanf() is rather difficult
    to use correctly for reading user input and typically it's a lot
    easier to simply read in a whole line with fgets() and then to
    take that apart as necessary. That's also true if you e.g. want
    an integer as input from the user - if the user types in some-
    thing else it's difficult to catch that correctly with scanf()
    and way much easier to deal with if you have the whole line and
    can analyze it carefully. It's rumored that there are only very
    few people who got the hang of using scanf() to savely read in
    user input;-)
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Feb 8, 2005
    #6
  7. Peter

    Flash Gordon Guest

    Dmitry wrote:

    Please don't top post. Replies belong after or interleaved with the text
    you are replying to.

    > Using GNU libc, 'getline()' is a best choice (IMHO).


    Not round here it isn't. Here we deal with standard C, not
    implementation specific extensions. After all (or any of), what makes
    you think the all the platforms the OP wants to use this on have GNU
    libc? Especially since the post was from a Windows machine?

    > Another way is 'scanf' with the next pattern:
    >
    > scanf("%[^\n\r]", input_string);
    >
    > i.e "everything before newline or carriage return symbol". Beware of
    > buffer overflow ;)


    Nor accurate would be don't do that ever. You should *never* read a
    string from stdin without limiting the length. Also, fgets is designed
    to do what the OP wants, i.e. read a line of text.

    > Peter wrote:
    >
    >> "those who know me have no need of my name"
    >> <> wrote in message
    >> news:...
    >>
    >>> in comp.lang.c i read:
    >>>
    >>>
    >>>> scanf("%s", input_string);

    >>
    >>
    >> What's the function I'm looking for then to take in any length of
    >> string (whether white space or not)?


    Look up fgets in your C reference, and if you don't have one buy a copy
    of K&~R2. Also read the comp.lang.c FAQ which you can find easily enough
    with Google.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Feb 8, 2005
    #7
  8. Peter

    CBFalconer Guest

    Flash Gordon wrote:
    >> Peter wrote:
    >>

    .... snip ...
    >>>
    >>> What's the function I'm looking for then to take in any length
    >>> of string (whether white space or not)?

    >
    > Look up fgets in your C reference, and if you don't have one buy
    > a copy of K&~R2. Also read the comp.lang.c FAQ which you can find
    > easily enough with Google.


    The OP can also download and use the freely available ggets
    routine, written in purely standard C, which avoids most of the
    nuisances involved with fgets and the insecurities of gets.

    <http://cbfalconer.home.att.net/download/ggets.zip>

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
     
    CBFalconer, Feb 9, 2005
    #8
  9. Flash Gordon <> writes:
    [...]
    > Look up fgets in your C reference, and if you don't have one buy a
    > copy of K&~R2. Also read the comp.lang.c FAQ which you can find easily
    > enough with Google.


    Surely Mr. Ritchie deserves to be complimented, not complemented.

    --
    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, Feb 9, 2005
    #9
  10. -berlin.de writes:
    [...]
    >> scanf("%s", input_string);
    >> printf("The output is %s\n", input_string);
    >> exit(0);

    >
    > A "return 0;" or "return EXIT_SUCCESS;" will do here perfectly well,
    > no reason to kill the program;-)


    Within main(), "return 0;" and "exit(0);" are very nearly identical.
    I tend to prefer return, but there's nothing wrong with using exit().

    --
    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, Feb 9, 2005
    #10
  11. Peter

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > Flash Gordon <> writes:
    > [...]
    >> Look up fgets in your C reference, and if you don't have one buy a
    >> copy of K&~R2. Also read the comp.lang.c FAQ which you can find easily
    >> enough with Google.

    >
    > Surely Mr. Ritchie deserves to be complimented, not complemented.


    K&++R2?
     
    Ben Pfaff, Feb 9, 2005
    #11
  12. Peter

    dandelion Guest

    "Ben Pfaff" <> wrote in message
    news:...
    > Keith Thompson <> writes:
    >
    > > Flash Gordon <> writes:
    > > [...]
    > >> Look up fgets in your C reference, and if you don't have one buy a
    > >> copy of K&~R2. Also read the comp.lang.c FAQ which you can find easily
    > >> enough with Google.

    > >
    > > Surely Mr. Ritchie deserves to be complimented, not complemented.

    >
    > K&++R2?

    K&R2D2?
     
    dandelion, Feb 9, 2005
    #12
  13. On Wed, 09 Feb 2005 05:37:02 GMT, Keith Thompson
    <> wrote:

    > Flash Gordon <> writes:
    > [...]
    >> Look up fgets in your C reference, and if you don't have one buy a
    >> copy of K&~R2. Also read the comp.lang.c FAQ which you can find easily
    >> enough with Google.

    >
    > Surely Mr. Ritchie deserves to be complimented, not complemented.


    Ooh, nice wordplay! Although I keep reading K&R2 as K2R2 and thinking
    it's a robot...

    Chris C
     
    Chris Croughton, Feb 9, 2005
    #13
  14. Peter

    John Smith Guest

    Keith Thompson wrote:

    > Flash Gordon <> writes:
    > [...]
    >
    >>Look up fgets in your C reference, and if you don't have one buy a
    >>copy of K&~R2. Also read the comp.lang.c FAQ which you can find easily
    >>enough with Google.

    >
    >
    > Surely Mr. Ritchie deserves to be complimented, not complemented.
    >

    Yes, he has already been complemented by Mr. Kernighan.
     
    John Smith, Feb 9, 2005
    #14
  15. On Tue, 8 Feb 2005 21:52:32 +0100, "Peter" <> wrote:
    <snip>
    > What's the function I'm looking for then to take in any length of string
    > (whether white space or not)?
    >

    Others have already answered what you probably meant to ask, which is
    how to input (up to) a line of data possibly containing space, or
    perhaps "linear whitespace" or "horizontal whitespace" which includes
    tab also. However, the definition of whitespace in C, and also in
    (most?) Internet standards, includes newline, and CR and VT and FF. So
    your question as stated is actually to read an entire (text) file into
    memory -- or an entire TCP (e.g. HTTP) datastream, if on a system
    where sockets are interchangeable with files (i.e. Unix) or otherwise
    supported by stdio. If that's what you want, the simplest way is just

    char buf [BIGENUF];
    size_t len;
    len = fread (buf, 1, sizeof buf /* or BIGENUF */, fp_eg_stdin);
    /* if you want to use the result as a string in C: */
    /* do the read for (sizeof buf) -1 (parens not required
    but shown for clarity) aka BIGENUF -1 and then */
    buf [len] = '\0' /* or just 0 */

    If you can't determine in advance (at compile time, or in C99 or GCC
    for a local=automatic buffer at declaration which must be before the
    read) a buffer size that will be sufficiently large, you must either:

    - determine file size, which can't be done fully portably, FAQ 19.12
    at the usual places and http://www.eskimo.com/~scs/C-faq/top.html ,
    then malloc that much space, checking for failure (return == NULL) and
    handling as appropriate, otherwise read the data

    - or, malloc a buffer of some "normal" size and try reading that much,
    and if that doesn't reach EOF, realloc to a larger size and read more
    etc. until either you get it all or realloc fails (out of memory)

    In both cases plus a byte for a null terminator if you want a string.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Feb 14, 2005
    #15
    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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    524
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    357
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,727
    Merrill & Michele
    Oct 19, 2004
  4. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,285
    August Karlstrom
    Sep 26, 2008
  5. Sunjay Varma
    Replies:
    4
    Views:
    367
    John Roth
    Mar 10, 2011
Loading...

Share This Page