Text file parsing

This is a discussion on Text file parsing within the C Programming forums, part of the General Programming Boards category; I want to learn how to parse text files. Especifically, I want to work with .obj files. I have read ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    30

    Text file parsing

    I want to learn how to parse text files. Especifically, I want to work with .obj files.

    I have read about the the basics of the obj format, but I'm wondering what standard library functions should I use to read it efficiently. For instance, I want to read the following vertexes

    v 5.672271 1.802055 -5.287645
    v 6.117182 1.802056 5.492141
    v -5.000000 1.803496 4.999999

    into this struct:

    Code:
    struct vertex
    {
        float x,y,z;
    } vertex_data[num_of_vertexes];
    Could someone give me an example of what functions I could use to do it?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What's wrong with fscanf?

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just a note from the peanut gallery here, but this sounds like a "roll your own", to me.

    I've never heard of a standard library in C for this.

    Sounds too simple to build a library just to import some a char, and 3 numbers.

    Do you want help putting the values into your struct w/o a library?

  4. #4
    Registered User
    Join Date
    Aug 2008
    Posts
    30
    By standard library I mean stdio, stdlib, etc. I'm an occasional programmer so I'm still learning about so sometimes I'm just not sure which function to use. I did read the scanf, fscanf and sscanf documentation, that was very helpful.

    After looking at some examples of parsing text files, here's what I am using:

    Code:
        char line[2048];
    
        while (fgets(line, 2048, fp))
        {          
    
        /* get a face */
        if (!strncmp(line, "f ", 2))
        {
            fCount++;                           
    	sscanf(line+2, "%d/%d/%d %d/%d/%d %d/%d/%d", &args ... )
        }
    However that leads me with a problem. If the program encounters a face like
    f 1/2/3 3/4/5 6/7/8 it reads fine, however f 1//3 3//5 6//8 will trash my data harvest. Any ideas how to deal with this?

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Hmm. You can include dud characters in a (s)scanf line with the * flag. That means those items are not placed into a variable:

    Code:
    scanf("%d%*[/]%d", &x,&y)
    x and y get a number, %*[/] should read any number of / and discard them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    use fscanf or strtok

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    use combination oif fgets and sscanf functions

    f 1/2/3 3/4/5 6/7/8 it reads fine, however f 1//3 3//5 6//8 will trash my data harvest. Any ideas how to deal with this?
    Code:
        if( sscanf( str, "%*c %d%*[/]%d %d%*[/]%d%*s", &i,&j,&k,&l) == 4 )
            printf("%d %d %d %d\n", i,j,k,l );
    ~ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( (c=fgetc( in )) != EOF )
    {
        if( isdigit( c ) )
        {
            store( c );
        }
    }
    Or be lazy, since all you seem to care about in your second post is the individual digits.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Parsing specific data from one text file to another..
    By chops11 in forum C Programming
    Replies: 2
    Last Post: 09-13-2005, 10:50 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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