Changes between Version 2 and Version 3 of DataParser

2017-10-23T16:29:54Z (3 months ago)

Add one new section.


  • DataParser

    v2 v3  
    107107== Reading the data ==
     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:
     111while( d.getline() )
     113    long ilo, ihi;
     114    d.getToken(ilo);
     115    d.getToken(iHi);
     116    double Aul;
     117    d.getToken(Aul);
     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:
     122char c;
     124string s;
     127Note that there is a separate method for reading quoted strings that are allowed to contain whitespace. That will be discussed later.
     129If a line contains a fixed number of data items that you want to read in one call, you can use:
     131double temps[10];
     135Sometimes you do not know in advance how many data items are on a line. You can handle that using the following method:
     137vector<double> temps;
     138double token;
     139while( d.getTokenOptional(token) )
     141    temps.emplace_back(token);
     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.
    109146== Skipping parts of the line ==