#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Posts
    79
    Rep Power
    12

    help to parse a text file


    Below is an example of a textfile that I am trying to parse. This is the exact contents of the file:

    //my file format

    "group1"
    20
    25.16084480285645 113.0293579101562 131.2838287353516
    26.85208511352539 135.4442749023438 136.4091949462891
    26.34756469726562 135.4442749023438 131.1695404052734
    25.66536521911621 113.0293579101562 136.5234680175781
    49.81016540527344 128.5846405029297 129.3254699707031
    44.98514938354492 136.6223602294922 132.7676849365234
    51.37056732177734 128.5846405029297 131.2195434570312
    44.67952728271484 136.6223602294922 129.5936737060547
    25.16084480285645 113.0293579101562 131.2838287353516
    51.37056732177734 128.5846405029297 131.2195434570312
    25.66536521911621 113.0293579101562 136.5234680175781
    49.81016540527344 128.5846405029297 129.3254699707031
    26.85208511352539 135.4442749023438 136.4091949462891
    51.37056732177734 128.5846405029297 131.2195434570312
    26.85208511352539 135.4442749023438 136.4091949462891
    44.67952728271484 136.6223602294922 129.5936737060547
    26.34756469726562 135.4442749023438 131.1695404052734
    44.98514938354492 136.6223602294922 132.7676849365234
    26.34756469726562 135.4442749023438 131.1695404052734
    49.81016540527344 128.5846405029297 129.3254699707031



    Basically the first line:
    //my file format

    is just a label so that needs to be skipped.

    The next line:
    "group1"

    Is the name of the following list of vertexes

    The next line:
    20

    is the amount of vertexes listed in the next section

    and the final bit is the list of vertexes.

    I can read the vertexes from the file fine but the problem that I am having has to do with getting to the "group" line. Say this file had three groups listed in it and I wanted to get to the third group in the list. Is there a way to skip to a particular section based on the group number? Or say that there was other info in the file that I wanted to get to but wasnt a set lenght from the beginging? Does one have to read an entire file to load it or can one load individual bits and if so how does one navigate to them in the file?

    Also does any one know any good tutorials or doco on fscanf. i need to work out how to serperate the 1 from "group1".

    Thanks in advance.
    Last edited by phantom_turtle; June 18th, 2003 at 01:28 AM.
  2. #2
  3. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    I would load the whole file into a dynamically allocated buffer.

    Set a pointer to the begining and then use library functions like strstr()

    something like
    Code:
    //open file
    //find size
    //alloc char buffer (sFileBuffer)
    
    char *pString=NULL,*pGroup=NULL;
    
    pString=sFileBuffer;
    while( !bFound && !bEOF )
    {
         pGroup = strstr( pString ,"group" );
         if(pGroup != NULL) //found a group
         {
               //if we find a group get the number after it
                *pGroup += lstrlen("group");
                if(atoi(pGroup) == iGroupNum )
                   bFound = TRUE;
                 else //set the pointer to the new position
                  {
                          *pString=*pGroup;
                           // reset  the pointer for next loop
                          *pGroup=NULL;
                   }
           }
            else //no more group
                 bEOF = TRUE;
    }
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa

IMN logo majestic logo threadwatch logo seochat tools logo