why msvcrt.printf show the first char only?

I

installman

from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello world!\n"
print(msvcrt.printf("Testing: %s", message_string))

when running in eclipse, the result is:
1
T

when running in IDLE, then result is:
1

why is that?
 
N

Nobody

from ctypes import *
msvcrt = cdll.msvcrt
message_string = "Hello world!\n"
print(msvcrt.printf("Testing: %s", message_string))

when running in eclipse, the result is:
1
T

when running in IDLE, then result is:
1

why is that?

Odd. I get 22 when running from IDLE.

Also, when using the console, it actually prints the text. I suspect that
stdout gets block-buffered when using an IDE. I can't see any way to get a
reference to stdout, so you can't fflush() it.
 
G

Gabriel Genellina

Odd. I get 22 when running from IDLE.

Also, when using the console, it actually prints the text. I suspect that
stdout gets block-buffered when using an IDE. I can't see any way to get a
reference to stdout, so you can't fflush() it.

Launch IDLE from the command line and you'll see the text output.

To the OP: I bet your Eclipse runs Python 2.x and IDLE is 3.x.

In Python 3.x, "Test..." is a Unicode string, internally represented
using two bytes per character. (In contrast, in Python 2.x, "Test..."
is a byte string, and u"Test..." is unicode). All ASCII characters
have a 0 as their second byte in its internal representation. printf
expects a byte string, and stops as soon as it sees the '\0' following
the 'T' in 'Testing'. Either use wprintf("Testing..."), or encode the
Unicode object into a byte string before calling:
printf("Testing...".encode(sys.stdout.encoding)), or tell ctypes about
the right parameter type:

printf = msvcrt.printf
printf.argtypes = [c_char_p]
printf("Testing\n")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top