我对ctypes有一个有趣的问题; 虽然它似乎在常规python脚本中工作,但当我在带有printf()的解释器中使用它时,它会在字符串本身之后打印字符串的长度.演示:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> libc = CDLL("libc.so.6")
>>> libc.printf("Test")
Test4
>>> int = 55
>>> libc.printf("Test %d", int)
Test 557
>>> int = c_int(55)
>>> libc.printf("Test %d", int)
Test 557
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?
From the printf(3) man page:
Upon successful return, these functions return the number of characters printed (not including the trailing
’\0’used to end output to strings).
python解释器printf()在您调用它之后显示返回代码.由于\n字符串末尾没有换行符,因此打印输出后会立即打印长度.请注意,仅当您以交互方式使用python时,才会在脚本中执行此操作.
您可以使用作业隐藏此内容:
ret = libc.printf("Test\n")
Run Code Online (Sandbox Code Playgroud)