I/O Format Specifiers for Reading Integers
Integer Data — %d (and variants)
Section titled “Integer Data — %d (and variants)”An integer argument has a format specifier of the following form:
%[*] [488.2 type] [width] [delimiter [array size] ] [$S] [b | h | l | I] [d | o | x]
| 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 expected to be 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 int or short elements, 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 integers. 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 interpreted as a pointer to a SAFEARRAY of integers. |
length modifier | If no length modifier is specified, then the argument is assumed to be a pointer to a long integer (32-bits). b - argument is a pointer to a BYTE (8-bit) integer.h - argument is a pointer to a short (16-bit) integer.l - argument is a pointer to a long (32-bit) integer.I - argument is a pointer to a __int64 (64-bit) integer. |
type | d - number is interpreted as a decimal integer.o - number is interpreted as an octal integer.x - number is interpreted as a hex integer. |
Example — Integer basics
Section titled “Example — Integer basics”HRESULT hr = S_OK;
int nArg1;int nArg2;int nArg3;
// Instrument sends:// "8, 100, 42"hr = io.Scanf(_T("%d,%d,%d"), &nArg1, &nArg2, &nArg3); // nArg1 == 8 // nArg2 == 100 // nArg3 == 42
// Discard the second number in the list// Instrument sends:// "8, 100, 42"hr = io.Scanf(_T("%d,%*d,%d"), &nArg1, &nArg2); // nArg1 == 8 // nArg2 == 42Example — Integer length modifiers
Section titled “Example — Integer length modifiers”HRESULT hr = S_OK;
long lArg1;short sArg2;
// Instrument sends:// "8, 100"hr = io.Scanf(_T("%ld,%hd"), &lArg1, &sArg2); // nArg1 == 8 // nArg2 == 100Example — Integer field width
Section titled “Example — Integer field width”HRESULT hr = S_OK;
int nArg1;int nArg2;
// Field width controls how much is read for each argument// Instrument sends:// "123456789"hr = io.Scanf(_T("%3d%4d"), &lArg1, &sArg2); // nArg1 == 123 // nArg2 == 4567
// Field width supplied using variables// Instrument sends:// "123456789"int nWidth1 = 3;int nWidth2 = 4;hr = io.Scanf(_T("%#d%#d"), &nWidth1, &lArg1, &nWidth2, &sArg2); // nArg1 == 123 // nArg2 == 4567Example — Integer arrays
Section titled “Example — Integer arrays”HRESULT hr = S_OK;
int rgData[5];
// Comma-separated list of 3 elements// Instrument sends:// "123,456,789"hr = io.Scanf(_T("%,3d"), rgData); // rgData[0] == 123 // rgData[1] == 456 // rgData[2] == 789
// Mixed delimiters, count supplied as a variable// Instrument sends:// "123,456:789;321"int nCount = 5;hr = io.Scanf(_T("%(;,:)#d"), &nCount, rgData); // rgData[0] == 123 // rgData[1] == 456 // rgData[2] == 789 // rgData[3] == 321
// 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:// "123,456,789"SAFEARRAY* psa = NULL;hr = io.Scanf(_T("%,$Sd"), &psa); // psa[0] == 123 // psa[1] == 456 // psa[2] == 789
// ... use SAFEARRAY result
// IMPORTANT: SAFEARRAY must be destroyedhr = ::SafeArrayDestroy(psa);Example — IEEE 488.2 format numbers
Section titled “Example — IEEE 488.2 format numbers”HRESULT hr = S_OK;
int nArg1;int nArg2;int nArg3;
// Read numbers in IEEE 488.2 hex format// Instrument sends:// "#H34E8,#H12B,#HFE"hr = io.Scanf(_T("%@Hd,%@Hd,%@Hd"), &nArg1, &nArg2, &nArg3); // nArg1 == 0x34E8 // nArg2 == 0x12B // nArg3 == 0xFE