Changes between Version 2 and Version 3 of DataParser


Ignore:
Timestamp:
2017-10-23T16:29:54Z (4 weeks ago)
Author:
peter
Comment:

Add one new section.

Legend:

Unmodified
Added
Removed
Modified
  • DataParser

    v2 v3  
    107107== Reading the data ==
    108108
     109The basic method for reading data from a line is getToken(). This can be used to read data of any type, as long as operator>>() is defined for that type (either by the system or by you). For the commonly used types double, realnum, and most integer types, specializations have been created that scan the numbers more efficiently than the standard system routines can. This is transparent for the user. Below is an example where we read two integers and a double from every line in a file:
     110{{{
     111while( d.getline() )
     112{
     113    long ilo, ihi;
     114    d.getToken(ilo);
     115    d.getToken(iHi);
     116    double Aul;
     117    d.getToken(Aul);
     118}
     119}}}
     120On each call the method getToken() will skip leading whitespace and then read the number until it hits another whitespace character. If the text is not fully used in the conversion, an error will be generated and the code will abort. This method is not limited to numeric tokens. It can e.g. also read characters or a string:
     121{{{
     122char c;
     123d.getToken(c);
     124string s;
     125d.getToken(s);
     126}}}
     127Note that there is a separate method for reading quoted strings that are allowed to contain whitespace. That will be discussed later.
     128
     129If a line contains a fixed number of data items that you want to read in one call, you can use:
     130{{{
     131double temps[10];
     132d.getToken(temps,10);
     133}}}
     134
     135Sometimes you do not know in advance how many data items are on a line. You can handle that using the following method:
     136{{{
     137vector<double> temps;
     138double token;
     139while( d.getTokenOptional(token) )
     140{
     141    temps.emplace_back(token);
     142}
     143}}}
     144The routine getTokenOptional() will return true as long as reading another number was successful. It will not generate an error if reading the number fails, and return false instead.
     145
    109146== Skipping parts of the line ==
    110147