Thread: storing spaces in C

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    60

    storing spaces in C

    I am trying to allow the user to enter data through my program so it will give them an output, though I need it to be able to accept blank spaces.

    I am trying to use the scanf function using S, a character string. is it possible to get this working or do I need to approach it another way.

    Thanks for the help in advance
    Aaron

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am trying to use the scanf function using S, a character string.
    scanf uses whitespace as a delimiter, so trying to read strings with embedded whitespace requires an advanced knowledge of format modifiers. You shouldn't be using scanf for string input to begin with (or at all, in most cases). A combination of fgets and sscanf is much better. fgets to read a line, and sscanf to break it up into different types if necessary.
    My best code is written with the delete key.

  3. #3
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    You want to avoid scanf as Prelude said. Plus whats with the name? Just joking
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-22-2008, 12:51 AM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 11:53 AM
  4. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 02:42 PM
  5. Replies: 5
    Last Post: 06-30-2003, 12:52 PM