Thread: Space bar?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103

    Space bar?

    When I try to save a string lets say a 20 char sting, and lets say I use scanf to get the string and I type in "John Smith" then I printf the strong, why does it only print until the space? (it only prints this "John").. any answers/ ideas on how to prints strings the user inputs that include spaces?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    scanf is a "bad function" for user input.

    fgets() is a much better function to base user input from for several reasons, the main one being that fgets() is "safe" (it doesn't overflow the input buffer).

    Once you have read the string in, you may need to parse the string with something like sscanf() or manual parsing.

    --
    Mats

  3. #3
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    that doesn't really help me? your saying if I use fgets instead of scanf() then it will save the space bar stuff.. and what is sscanf() I see people using it, but what is it?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Does your computer not have a help/man page for the C functions? Try google "man sscanf" perhaps?

    Likewise "man fgets"


    --
    Mats

  5. #5
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    well I know what fgets does and I read about sscanf on google.. but it confused the hell outa me so... for now if there isn't another way... I guess I am stuck.. how stupid is that tho. to not make it to where is can scan and print spaces...

  6. #6
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    fgets() does help, but not for that reason. As you have noticed the *scanf() family extracts only a word with %s, fgets() reads until it hits a newline/EOF/buffer runs out.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    haha I just tryed gets() and it works.. hahaha

  8. #8
    Registered User
    Join Date
    Jul 2007
    Location
    Texas
    Posts
    103
    And thanks, i didn't know it only takes one word

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    haha I just tryed gets() and it works
    No no, not gets. fgets.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > haha I just tryed gets() and it works
    That's like saying "I played a round of Russian Roulette". Keep doing it, and it will end badly.

    Use fgets() and read the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Out of space when compiling kernel
    By NuNn in forum Linux Programming
    Replies: 3
    Last Post: 04-01-2009, 02:43 PM
  2. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  3. progress bar newbie
    By WaterNut in forum Windows Programming
    Replies: 18
    Last Post: 08-09-2004, 01:42 PM
  4. Replies: 12
    Last Post: 05-17-2003, 05:58 AM