scanf to include white spaces

Discussion in 'C Programming' started by Jonathan Ng, Aug 12, 2004.

  1. Jonathan Ng

    Jonathan Ng Guest

    Hi,

    I was wondering if there was a way to include the white spaces in a string.

    Currently, I am using:
    scanf("%s", &input);

    However, this doesn't include the 'space' character or any other white
    spaces. Is there a way I can include the 'space' character rather than skip
    in.

    Thanks in advance.
    Jonathan Ng, Aug 12, 2004
    #1
    1. Advertising

  2. Jonathan Ng

    Michael Mair Guest

    Hiho,

    > I was wondering if there was a way to include the white spaces in a string.
    >
    > Currently, I am using:
    > scanf("%s", &input);
    >
    > However, this doesn't include the 'space' character or any other white
    > spaces. Is there a way I can include the 'space' character rather than skip
    > in.


    Yep. Use a list of permitted/prohibited characters.
    Example:

    > cat scanblank.c

    #include <stdio.h>

    int main(void)
    {
    char string[80];

    puts("first test. Enter string: ");
    scanf("%[^\t\n]",string);
    puts(string);
    while(getchar()!=(int)'\n');

    puts("\nsecond test. Enter string: ");
    scanf("%[ a-zA-Z0-9+*/-]",string);
    puts(string);

    return 0;
    }

    >./scanblank

    first test. Enter string:
    hello, world !
    hello, world

    second test. Enter string:
    abcde j, fghk
    abcde j

    >


    In the first test, we scan for everything with exception of '\t' and
    '\n'. Between d and !, I entered a tabulator. In the second test, we
    scan for
    a,b,c,..,z,A,B,C,..,Z,0,1,2,..,9,+,*,/,-
    You always read at least one of the permitted/non-prohibited characters.
    The hyphen inbetween marks a range rather than meaning the hyphen
    character. The list must be nonempty.
    To scan (or scan not) for ], you have to put it immediately after [ or
    [^, respectively.

    Remark: I am not checking the return values of puts and scanf.
    Especially the latter might be necessary for you.


    Cheers
    Michael
    Michael Mair, Aug 12, 2004
    #2
    1. Advertising

  3. Jonathan Ng

    -berlin.de Guest

    Jonathan Ng <> wrote:
    > I was wondering if there was a way to include the white spaces in a string.


    > Currently, I am using:
    > scanf("%s", &input);


    This looks rather strange. When you have a '%s' format specifier the
    variable must be an array pointer. And if 'input' is one than you
    would need

    scanf( "%s", input );

    And I have problems to imagine a situation where 'input' could be
    anything else than an array.

    > However, this doesn't include the 'space' character or any other white
    > spaces. Is there a way I can include the 'space' character rather than skip
    > in.


    No, scanf() stops at white space. And I would guess that you would be
    a lot better of using fgets() here. First of all, when using scanf()
    you have no way to check that the string the user enters isn't longer
    than there is place in your 'input' array - if that happens scanf()
    will happily write beyond the end of the array with impossible to
    forsee consequences (including everything to seem to work nicely).
    And then you get a whole line of input (or as many characters as fit
    into the buffer) and you than can split it up however you need and
    aren't handicapped by the limitations of scanf().

    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
    -berlin.de, Aug 12, 2004
    #3
  4. Jonathan Ng wrote on 12/08/04 :
    > Hi,
    >
    > I was wondering if there was a way to include the white spaces in a string.
    >
    > Currently, I am using:
    > scanf("%s", &input);


    This '&' is suspicious. How is 'input' defined?

    > However, this doesn't include the 'space' character or any other white
    > spaces. Is there a way I can include the 'space' character rather than skip
    > in.


    For many reasons (including your issue), it's better to get a line from
    the user with fgets(). BTW, it's a FAQ.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

    "C is a sharp tool"
    Emmanuel Delahaye, Aug 12, 2004
    #4
  5. Jonathan Ng

    pete Guest

    Jonathan Ng wrote:
    >
    > Hi,
    >
    > I was wondering if there was a way to include the white spaces in a string.
    >
    > Currently, I am using:
    > scanf("%s", &input);
    >
    > However, this doesn't include the 'space' character or any other white
    > spaces.
    > Is there a way I can include the 'space' character rather than skip
    > in.


    /* You can use Pop's Device. */

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

    #define LENGTH 65
    #define str(x) # x
    #define xstr(x) str(x)

    int main(void)
    {
    int rc;
    char array[LENGTH + 1] = "";

    fputs("Enter a string with spaces:", stdout);
    fflush(stdout);
    rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
    if (!feof(stdin)) {
    getchar();
    }
    while (rc == 1) {
    printf("Your string is: \n%s\n", array);
    fputs("Hit Enter key to end, or enter "
    "a string with spaces:", stdout);
    fflush(stdout);
    rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
    if (!feof(stdin)) {
    getchar();
    }
    }
    return 0;
    }

    --
    pete
    pete, Aug 12, 2004
    #5
  6. Jonathan Ng

    Dan Pop Guest

    In <> Emmanuel Delahaye <> writes:

    >Jonathan Ng wrote on 12/08/04 :
    >> Hi,
    >>
    >> I was wondering if there was a way to include the white spaces in a string.
    >>
    >> Currently, I am using:
    >> scanf("%s", &input);

    >
    >This '&' is suspicious. How is 'input' defined?
    >
    >> However, this doesn't include the 'space' character or any other white
    >> spaces. Is there a way I can include the 'space' character rather than skip
    >> in.

    >
    >For many reasons (including your issue), it's better to get a line from
    >the user with fgets(). BTW, it's a FAQ.


    Pray tell, why is fgets() better than scanf for the OP's issue?

    As a matter of fact and excepting gets(), fgets() is *always* the
    worst solution, when bullet proof behaviour is desired/need.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 16, 2004
    #6
    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. \Dandy\ Randy
    Replies:
    2
    Views:
    1,523
    Eric J. Roode
    Jul 21, 2003
  2. Niels Dybdahl

    Color.white vs. Color.WHITE

    Niels Dybdahl, Oct 6, 2004, in forum: Java
    Replies:
    3
    Views:
    413
    Chris Smith
    Oct 6, 2004
  3. Ben C
    Replies:
    6
    Views:
    2,123
    Leif K-Brooks
    Jan 28, 2007
  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,312
    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:
    651
    Richard Bos
    May 2, 2006
Loading...

Share This Page