Thread: parsing a text data as...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    141

    parsing a text data as...

    Hi,

    I've a data file as:

    AB01 \t G01550 \t 4.0153- \t N001b \n
    AB03 \t G01560 \t 4.0173+ \t N001w \n
    .
    .
    .

    I want to store only the numbers associated with G. I mean, I want to store 1550, 1560 ...
    And also I want to store the associated + or -.

    I'll be obliged if anyone could suggest some means to parse in this manner?

    My algo may be :
    After the 1st tab I'll reject G and store that in an id_array
    And I'll also store the character before the 3rd tab in the sign_array...

    Do I sound right? Also how can I implement this?

    Thanks,
    Angkar

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Read in a buffer with fgets() and parse it (possibly with sscanf()) at will.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks a ton.
    Now I've got G01550, G01560 how can I rip the G off and store the rest in an int array? Any functions?

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You can get rid of the first character just by using &array[1] or memmove().

    I would suggest sscanf() again.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    141
    Thanks...

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Why use sscanf? Just use fscanf.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That's what I usually suggest, but someone usually corrects me and says "fgets()+sscanf() is better" . . .

    I guess you can use fscanf() unless you need the buffer later.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > Why use sscanf? Just use fscanf.
    Because with very little effort, fgets() + sscanf() allows you to recover from badly formatted files.

    Using fscanf(), if the format of the file breaks down, you end up with a big problem of how to recover.
    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. Replies: 9
    Last Post: 11-09-2006, 07:31 PM
  2. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM