TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: ROY LEBLANC
from: CAMERON CLARK
date: 1997-12-05 15:27:00
subject: Re: Parsing a file

RL> Could some one tell me how I can use C++ to input a line
RL> of ASCII-delimited text from a file and parse it into an
RL> array/structure/structured array(?)?
RL> 
RL>  A typical line might look like:
RL> 
RL>  "89765B","William","McIntire","70501",724,2,27.85
    Pick your syntax, first.
    What are your delimiters? space,commas
    Heres a common example for space,comma,quoted delimited text.
    while (s[index] = ' ') index++;  // skip zero or more spaces
    switch(s[index]) {
    case ',' : { index++; break}     // skip a comma
    case '"' : { index2 = index;
                 while (s[index2] != '"') index2++;
                 //index to index2-1 is the range of a "" string
                 break;
               }
    default : {  index2 = index;
                 while (isDigit(s[index2])) index2;
                 //index to  index2-1 is the range of a number
                 break;
              }
    }
    
    Basic algorithm
    #1 - skip all spaces
    #2 - test the first character,
         it will tell you what kind of thing is coming next
    #3 - repeat until end of text
    
--- GEcho 1.00
---------------
* Origin: Digital OnLine Magazine! - (409)838-8237 (1:3811/350)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.