c ++ vs output debug information to the output window

OutputDebugString function

OutputDebugString (L "String"); (in the VS, the OutputDebugString OutputDebugStringW # DEFINE)
OutputDebugStringW (L "String");
OutputDebugStringA ( "string");

sprintf、swprintf、wsprintf、sprintf_s、snprintf、asprintf、vsprintf函数

sprintf, swprintf function : writing the formatted data string buffer
int sprintf (char * buffer, const char * the format [, argument] ...); Returns the number of bytes is stored in the buffer (bytes), and not including the terminating null character
int of swprintf (wchar_t
buffer, const wchar_t
the format [, argument **] ...); wide number of characters in the buffer memory in the return value, not including the terminating null character width
of characters to be written to point buffer- string buffer
format- the format control string
[argument] ... - optional parameters may beAny typeData (this function can be converted to numeric string)
format specifier:

  • % D formatted as a decimal integer with a sign to the output buffer
  • % U decimal format output buffer to an unsigned integer
  • % F as a floating point format to output buffer
  • % S formatted string from the buffer to
  • % C formatted output to the buffer as a single character
  • % E formatted output to a buffer in the form of floating-point exponential
  • % X formatted as an unsigned integer to a buffer output to the hexadecimal notation (af output lowercase)
  • % X formatted as an unsigned integer to a buffer output to the hexadecimal notation (af output uppercase)
  • 0% formatted as an unsigned integer in the output buffer to the octal
  • % G formatted to automatically select the appropriate representation to the output buffer
  • Description:
    may be inserted between the number "%" and the letters indicate the maximum field width. For example: % 3D represents an output three integers, not three right-aligned. % 9.2f represents an output field width of the float 9, which is 2 decimal places, 6 integer bits, representing a decimal point, not right-justified 9. % 8s represents an output string of eight characters, not eight characters right justified. If the width of the length field, bits integer or string exceeds described, the output of its actual length. However, floating-point, integer part if the bit width of an integer number of bits than described, will be the actual output bit integer; if the fractional part of decimal digits than the bit width of illustration, the width of the press illustrated in rounded output. Moreover, to add some 0 before the output value, it should be present before the add item 0 width. For example:
    % 04D indicates when the output value is smaller than a 4-bit, 0 to make up a total width of 4 in front. If a floating-point number representing the amount of character or integer output format, the decimal point represents the maximum width, in front of the decimal numbers represent the minimum width. For example: % 6.9s denotes a display character string with a length of not less than 9 and not greater than 6. If greater than 9, the content after the first nine characters will be deleted. L may be added between the lowercase "%" and the letter indicates the number of the output is long. For example: % LD represents long integer output, % LF represents an output double floating point number. You may control the output of the left or right aligned, i.e., added between a "%" and the letter "-" sign may indicate output left justified, or right justified. For example: % -7D represents an integer of 7 output left-justified. % -10s represents an output 10 characters left-aligned.
    wsprintf function: A series of values and character strings inputted to the buffer. wsprintf in fact divided into wsprintfA and wsprintfW. In VS, the DEFINE # wsprintf wsprintfW
    int __cdecl wsprintf (LPWSTR lpOut, LPCWSTR lpFmt, ...);
    the output buffer lpOut wsprintf the 1024 character limit, and the input and output of floating point numbers are not supported.
    swprintf (FPSString, L "% f ", FPS); // correct
    wsprintf (FPSString, L "% f ", FPS); // error ( "% f")
    sprintf_s function : a secure version of sprintf, designated buffer length to avoid sprintf () overflow risks, the main difference in the second parameter sprintf_s, the buffer size may be controlled
    int sprintf_s (buffer char *, size_t sizeofBuffer, the format const char *, [argument] ...);
    sizeOfBuffer- buffer size.
    snprintf function :
    int snprintf (char * str, size_t size, the format const char *, ...);
    role of size is restricted to not more than str write size bytes (including the trailing '/ 0').
    If the output because of limitations of size is truncated, the return value is "if there is enough space to store, the YingCan output the number of characters (not including the end of the string '/ 0') ", the value is equal to or larger than the size and size! That is, if the character string may be written" 0123456789ABCDEF "16 bits in total, but the size limit is 10, so snprintf () return value will be 16 instead of 10!
    asprintf function :
    int asprintf (STRP * char *, const char * fmt, ...);
    asprintf () can be said to be an enhanced version of sprintf () when the string of indefinite length, is very flexible, can be formatted in accordance with the length of the string, apply sufficient memory space. Furthermore, after use, must () Free space through the release. However, this is GNU the extended C library, instead of the standard C library or the POSIX
    vsprintf function :
    int vsprintf (char * String, char * the format, the va_list param);
    vsprintf () function arguments in character is located, the array elements in the array of have a percent sign (%) before this function is a string of "step by step [step-by-step]" performed after the first sequence%, the first array element is inserted;. in a second % after the insertion of a second array Su, and so on.

Example of use

C ++ hex char array output VS of the output window.

  char nSelectsat[6];   
  nSelectsat[5] = 0x60;
  nSelectsat[4] = 0x11;
  nSelectsat[3] = 0x90;
  nSelectsat[2] = 0x88;
  nSelectsat[1] = 0x80;
  nSelectsat[0] = 0x8a;
  //直接输出数组名
  char out0[128] = { 0 };
  sprintf(out0, "%x\r", nSelectsat);
  OutputDebugStringA(out0);//4fe7bc(nSelectsat数组地址)
   //直接输出*数组名
  char out00[128] = { 0 };
  sprintf(out00, "%x\r", *nSelectsat);
  OutputDebugStringA(out00);//ffffff8a(nSelectsat[0])
   //循环依次输出数组元素
  char out8[128] = { 0 };
  for (int i = 0; i < 6; i++)
  {
   sprintf(out8, "%x", nSelectsat[i]);
   OutputDebugStringA(out8);
  }//ffffff8affffff80ffffff88ffffff901160
  //数组元素先转换为无符号类型 (unsigned char),再输出
  //原因(参考8):%mx, 之类的格式输出,输出宽度不够,忽略输出宽度,直接全部输出。在输出前,转换为无符号类型,可以避免这种问题。
  //所有较短整型,在调用 printf 之类的输出流函数的时候,会转换为int 类型,
  //而 char 类型缺省实现,并不是 unsigned char,通常是 signed char;
  //转化为int 的时候,会做符号位扩展
  char out9[128] = { 0 };
  for (int i = 0; i < 6; i++)
  {
   sprintf(out9, "%x", (unsigned char)nSelectsat[i]);
   OutputDebugStringA(out9);
  }//8a8088901160

reference

1, OutputDebugString function
2, sprintf, swprintf Microsoft
3, c ++ data type conversion sprintf, of swprintf, wsprintf
. 4, wsprintf
. 5, the difference between c ++ in sprintf and sprintf_s of
6, sprintf, snprintf, asprintf, vsprintf function
7, finishing: C ++ in sprintf () function to use Xiangjie
8, an array of characters with printf function in hexadecimal output format?

Published 30 original articles · won praise 3 · Views 898

Guess you like

Origin blog.csdn.net/qq_42697866/article/details/103618372