c question... scaning the string

Discussion in 'OT Technology' started by strangerthanfiction, May 31, 2005.

  1. strangerthanfiction

    strangerthanfiction New Member

    Joined:
    May 15, 2004
    Messages:
    607
    Likes Received:
    0
    Location:
    Massachusetts
    "abc dd ffff dddd"

    scanf("%s", string_ptr);

    only yields "abc" , it dumps at the first blank space.
    any ideas how to get the whole string?
     
  2. G-n-P

    G-n-P New Member

    Joined:
    Apr 21, 2005
    Messages:
    5,126
    Likes Received:
    0
    Location:
    Tulsa
    scanf("%s", &string_ptr);

    that is how the scanf function should look

    also how are you defining string_ptr ?
     
  3. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Use the gets function.

    gets( string_ptr );
     
  4. RyanL

    RyanL OT Supporter

    Joined:
    Nov 30, 2004
    Messages:
    4,584
    Likes Received:
    0
    Location:
    St. Paul, MN
    skinjob is right, gets is what you want... scanf will scan into a string until it hits a space or a return char.
     
  5. turbo91

    turbo91 New Member

    Joined:
    Jun 14, 2002
    Messages:
    2,521
    Likes Received:
    0
    Location:
    San Diego, CA

    gets(), of course, should never be used in production software (gotta say it).
     
  6. samm

    samm Next in Line

    Joined:
    Dec 22, 2000
    Messages:
    2,630
    Likes Received:
    0
    Location:
    San Jose, CA
    True, though most good compilers will warn you not to use it:

    Code:
    [email protected]:~$ cat test.c
    #include <stdio.h>
    
    int main(int argc, char** argv) {
            char text[10];
            printf("hello world\n");
            gets(text);
    
            return 0;
    }
    
    [email protected]:~$ gcc -o test test.c
    /tmp/ccqAVsaX.o(.text+0x23): In function `main':
    : warning: the `gets' function is dangerous and should not be used.
    [email protected]:~$ 
    
     
    Last edited: Jun 1, 2005

Share This Page