[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.8 Parsing Numeric Values

To convert text input to a numeric value, it is often convenient to use the standard scanf, fscanf, or sscanf functions. Each of these functions requires you to specify the data type as part of the “format” string (for example, to read an integer and store it as an int, you might write scanf("%d", &x), but to store it as a long int, you might write scanf("%ld", &x)).

As with the printf-style macros described in the previous section, the macros listed below can be used to convert a string to a WFDB_Sample, WFDB_Time, WFDB_Frequency, or WFDB_Gain value, regardless of which standard C data types these represent. Each macro expands to a string constant (such as "d" or "ld"), which does not include the leading ‘%’ character. For example, to read a table of time and sample values, we might write:

 
WFDB_Time t;
WFDB_Sample v[2];
char buf[1000];
while (fgets(buf, sizeof(buf), stdin)) {
    if (sscanf(buf, "%"WFDB_Sd_TIME"%"WFDB_Sd_SAMP"%"WFDB_Sd_SAMP",
               &t, &v[0], &v[1]) == 3) {
        putvec(v);
    }
}

The following macros are defined in ‘<wfdb/wfdb.h>’:

MacroArgument typeFormat
WFDB_Sd_SAMPWFDB_Sample *Base 10
WFDB_Si_SAMPWFDB_Sample *Base 8, 10, or 16
WFDB_So_SAMPWFDB_Sample *Unsigned base 8
WFDB_Su_SAMPWFDB_Sample *Unsigned base 10
WFDB_Sx_SAMPWFDB_Sample *Unsigned base 16
WFDB_SX_SAMPWFDB_Sample *Unsigned base 16
WFDB_Sd_TIMEWFDB_Time *Base 10
WFDB_Si_TIMEWFDB_Time *Base 8, 10, or 16
WFDB_So_TIMEWFDB_Time *Unsigned base 8
WFDB_Su_TIMEWFDB_Time *Unsigned base 10
WFDB_Sx_TIMEWFDB_Time *Unsigned base 16
WFDB_SX_TIMEWFDB_Time *Unsigned base 16
WFDB_Se_FREQWFDB_Frequency *Decimal
WFDB_SE_FREQWFDB_Frequency *Decimal
WFDB_Sf_FREQWFDB_Frequency *Decimal
WFDB_Sg_FREQWFDB_Frequency *Decimal
WFDB_SG_FREQWFDB_Frequency *Decimal
WFDB_Se_GAINWFDB_Gain *Decimal
WFDB_SE_GAINWFDB_Gain *Decimal
WFDB_Sf_GAINWFDB_Gain *Decimal
WFDB_Sg_GAINWFDB_Gain *Decimal
WFDB_SG_GAINWFDB_Gain *Decimal

(The ‘x’ and ‘X’ formats are equivalent, as are the ‘e’, ‘E’, ‘f’, ‘g’, and ‘G’ formats, and are provided for symmetry with printf. For more information, see the documentation of your C compiler.)


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

PhysioNet (wfdb@physionet.org)