Reading spaces from file

This is a discussion on Reading spaces from file within the C Programming forums, part of the General Programming Boards category; Hello I know how to read most information from file for example if I have this in a file: Hammer;10;9.99 ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    48

    Reading spaces from file

    Hello

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

    Hammer;10;9.99
    Phillips Head Screwdriver;5;7.99
    Saw;2;9.95

    I would use:
    Code:
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    I'd try something like
    "%[^0-9]%d %lf"
    and then trim the first string
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 04:02 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21