TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CAMERON CLARK
from: THOMAS MAEDER
date: 1997-07-23 20:28:00
subject: exceptions in c++

CC>     Given situation: get a character and build a token until white s
CC>     is reached. White space can be an actual space, tab, or newline
CC>     character.
CC>     No token can be over MAX_TOKEN_LEN in length.
CC>     If you don't check for end of file you will run into an infinite
CC>     // assume inf = ifstream
CC>    char c;
CC>    do {
CC>       inf.get(c);
CC>       if (inf.gcount() != 0) {
CC>          if (current < MAX_TOKEN_LEN) {
CC>            token[current++] = c;
CC>          }
CC>       }
CC>    } while (((strrchr(" \t\n")==0) && (inf.gcount() != 0) &&
CC>             (current < MAX_TOKEN_LEN));
CC>    if ((inf.gcount() != 0)) {
CC>      inf.putback(c);
CC>    }
What about something like the following:
char token[MAX_TOKEN_LEN+1];
while (inf >> setw(sizeof token) >> token) 
    if (isspace(inf.peek())
        // process token here
    else
        // token too long
Feel free to use this design, but you should test it first! 
CC>    Notice that there will be _only_ one end of file reached per file
CC>    If I check every character read from the file 3 times I have a sl
CC>    slow, slow piece for code.
Have  you  tested the speed of exception handling? I haven't, neither,
but it's said to be slow, slow, slow.
Thomas
---
 þ MM 1.0 #0113 þ Have *I* been forgetful?  Sorry, I don't remember.
---------------
* Origin: McMeier & Son BBS (2:301/138)

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™.