Thread: Reading spaces from file

  1. #1
    Registered User
    Join Date
    Oct 2007

    Reading spaces from file


    I know how to read most information from file for example if I have this in a file:

    Phillips Head Screwdriver;5;7.99

    I would use:
    while (3 == fscanf(fp, "%[^;];%d;%lf\n", name, &qu, &amount))
    But how Can I scan a file that is divided with spaces? For example:

    Seinfeld 50 75
    Carson 40 20
    Leno 45 50
    Letterman 20 95

    I tried a couple of things, but I was unsuccessful, How Can I do this?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    I'd try something like
    "%[^0-9]%d %lf"
    and then trim the first string
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    First I would suggest you read the whole like using fgets, then attempt to parse it with sscanf.

    If the converted result isn't 3, then the file is already tidied up for you (because you read the whole line with fgets).
    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. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM