uniSPrintf()

Formats a string.

Synopsis

int uniSPrintf(string &s, string format [,<type1> var1 [,<type2> var2 ...]]);

Parameters

Parameter Description
s String. The characters are written to this string.
format Format. How are the characters displayed, for example, as a string %s
var1, var2, ... Values that are written.

Type signs

d The argument is represented as a decimal number
o The argument is represented as an octal number without leading zeros.
u The argument is displayed as an unsigned decimal number.
x The argument is represented as a hexadecimal number (without leading 0x). The letters abcdef are used.
X Like x. But the letters ABCDEF are used.
f

The argument float/double is represented as a decimal number.

[ - ] ddd.ddd is the default:

e

The argument float/double is represented as an exponent number in the format

[ - ] d.ddde dd

The exponent always contains at least two places. If the value is zero, the exponent is zero.

E Similar to e. Instead of e, E is used.
g Similar to for e, but dependent on the value. The conversion type e is only used when the exponent is less than -4 or greater than the accuracy.
G Similar tog. An E is output instead of an e.
s The argument is a string. Individual characters are output from the string until either a null character ("/0") has been reached or as many characters as required for accuracy.
Flag Meaning
+ Allows to allocate space for a sign (e.g. for a + or - sign before a number). The number 3.112 formatted with %+6.2f' = +3.11
- Left alignment (the right alignment is default) e.g. uniSPrintf(valueString, "%-8.2f", rawValueF3); In this case the value 2.1 (rawValueF3) is formatted with '%-8.2f' . Thus, the result is 2.10 left aligned (see examples at the end of this page).
blank A blank between the percent sign and the width (Number) allows to allocate space for the sign. e.g. % 6.2f. See examples at the end of this page.
Width Meaning
Number The minimum number of characters to be printed e.g. "%6d". If the value that should be printed is shorter than this number blanks are added.
0Number The minimum number of characters that should be printed. If the minimum is not reached then leading zeros are filled, e.g. the number 45.765998765 formatted with %07.2f' = 0045.77 means altogether 7 numbers are printed with leading zeros and two decimal places (.2). However, the result can have more then 7 characters.
.Number Specifies the number of decimal numbers (decimal places) that should be printed.

Return value

see Description

Errors

missing/incorrect arguments

Description

Writes outputs under the specified format to the string s and returns the number of characters written, in the event of errors a negative number.
Anmerkung: uniSPrintf works with 1 Byte (UTF8-Characters). For string formatting of other characters, use CTRL functions uniSubStr(), uniStrExpand(), uniStrFormat() and uniStrLen().

Example

The following example writes five letters into a text file.

const int STRING_COUNT = 2;    
main()
{
  int i,j;
  file f, f1;
  dyn_string russianValue;
  russianValue =makeDynString("русский","буcква");
  DebugN("Russian letters:", russianValue);
  //Opens two files for writing and reading
  f=fopen("C:/TEMP/tfile.TXT","w+");
  f1=fopen("C:/TEMP/tfile2.TXT","w+");
  //writes string values to the text file
  for (i=1;i<= STRING_COUNT;i++) uniFPrintfPL(f,"%.5s\n",
  russianValue [i]); //Сut the string to a length of 5 letters
  for (j=1;j<= STRING_COUNT;j++) fprintf(f1,"%.5s\n",
  russianValue [j]);//The text is not correctly written since
  fprinft is used
  fclose(f);
  fclose(f1);
}

Assignment

String functions

Availability

CTRL