I/O Format Specifiers for Reading Floating-Point Numbers
Floating-Point Data — %f (and variants)
Section titled “Floating-Point Data — %f (and variants)”A floating point argument has a format specifier of the following form:
%[*] [488.2 type] [width] [delimiter [array size] ] [$S] [l | L] [f | e | E | g | G]
| Modifier | Interpretation |
|---|---|
| Default Functionality | Characters are read from the device until an entire number is read. The number read may be in either IEEE 488.2 formats |
* (asterisk) | An asterisk acts as the assignment suppression character. The input is scanned but not assigned to any parameters and is discarded. |
488.2 type | The number is formatted in one of six standard IEEE 488.2 numeric formats. The supported formats are: @1 - IEEE 488.2 NR1 format (integer without any decimal point). Example: 123@2 - IEEE 488.2 NR2 format (number with at least one digit after the decimal point). Example: 123.45@3 - IEEE 488.2 NR3 format (floating-point number in exponential form). Example: 1.2345E-67@H - IEEE 488.2 hex format. Example: #HAF35B@Q - IEEE 488.2 octal format. Example: #Q71234@B - IEEE 488.2 binary format. Example: #B011101001 |
width | The input number will be stored in a field at least this wide. A # may be present in lieu of an integer precision modifier, in which case an extra int argument supplies the value. |
delimiter [array size] | The argument is an array of float, double, or long double element as dictated by the length modifier field. If the $S modifier is present, the argument is assumed to be a pointer to a SAFEARRAY of floating-point elements. array size specifies the maximum number of array elements to store in the user array argument. A # may be present in lieu of an integer array size modifier, in which case an extra int argument supplies the value. |
$S | The argument is a pointer to a SAFEARRAY of floating-point numbers. |
length modifier | l - argument is a pointer to a double.L - argument is a pointer to a long double. |
Example — Floating-point basics
Section titled “Example — Floating-point basics”HRESULT hr = S_OK;
double dArg1;double dArg2;double dArg3;
// Instrument sends:// "3.2, 1.53E-12, 0.021"hr = io.Scanf(_T("%le,%le,%le"), &dArg1, &dArg2, &dArg3); // dArg1 == 3.2 // dArg2 == 1.53E-12 // dArg3 == 0.021
// Instrument sends:// "3.2, 1.53E-12, 0.021"hr = io.Scanf(_T("%le,%*le,%le"), &dArg1, &dArg2); // dArg1 == 3.2 // dArg2 == 0.21Example — Floating-point length modifiers
Section titled “Example — Floating-point length modifiers”HRESULT hr = S_OK;
double dArg1;float fArg2;
// Instrument sends:// "3.14, 3E-4"hr = io.Scanf(_T("%lf,%f"), &dArg1, &fArg2); // dArg1 == 3.14 // fArg2 == 3E-4Example — Floating-point field width
Section titled “Example — Floating-point field width”HRESULT hr = S_OK;
double dArg;
// Field width controls how much is read for each argument// Instrument sends:// "12.3456"hr = io.Scanf(_T("%5le"), &dArg); // dArg == 12.34
// Field width supplied using variables// Instrument sends:// "12.3456"int nWidth = 5;hr = io.Scanf(_T("%#le"), &nWidth, &dArg); // dArg == 12.34Example — Floating-point arrays
Section titled “Example — Floating-point arrays”HRESULT hr = S_OK;
double rgData[5];
// Comma-separated list of 3 elements// Instrument sends:// "1.23,4.0E-56,0.789"hr = io.Scanf(_T("%,3le"), rgData); // rgData[0] == 1.23 // rgData[1] == 4E-56 // rgData[2] == 0.789
// Mixed delimiters, count supplied as a variable// Instrument sends:// "1.23;4.0E-56:0.789,-2"int nCount = 5;hr = io.Scanf(_T("%(;,:)#d"), &nCount, rgData); // rgData[0] == 1.23 // rgData[1] == 4E-56 // rgData[2] == 0.789 // rgData[3] == -2
// nCount is updated with actual countATLASSERT(nCount == 4);
// Comma-separated list. Argument is a SAFEARRAY.// An array element count is not required since Scanf allocates the memory// Instrument sends:// "1.23,4.0E-56,0.789"SAFEARRAY* psa = NULL;hr = io.Scanf(_T("%,$Sle"), &psa); // psa[0] == 1.23 // psa[1] == 4E-56 // psa[2] == 0.789
// ... use SAFEARRAY result
// IMPORTANT: SAFEARRAY must be destroyedhr = ::SafeArrayDestroy(psa);