C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 18th, 2003, 01:18 AM
phantom_turtle phantom_turtle is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 81 phantom_turtle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 14 m 21 sec
Reputation Power: 6
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.

Reply With Quote
  #2  
Old June 18th, 2003, 02:09 AM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 594 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 14 h 6 m 15 sec
Reputation Power: 21
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > help to parse a text file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT