#wprintf and %s for substrings

31 messages · Page 1 of 1 (latest)

jovial crescent
#

How do I print a substring with fixed length?

const wchar_t(&arrRef)[5] = L"ABCD";
    
    wprintf(L"%1s\n", arrRef);

this prints ABCD, the array takes 5 wide chars, each 2 bytes

fallow onyxBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

grizzled saffronBOT
#
Program Output
A
jovial crescent
#

dafuq

#

hold on, godbolt doesn't run windows

#

when I tried this it still prints ABCD

winged inlet
#

And the first argument should be a wstring

jovial crescent
winged inlet
#

Also, wchar_t might not be 2 bytes

#

its usually 2, but it doesn't have to be

jovial crescent
#

not to mention that if this wstring has \0 in it, wprintf needs to use fixed length to print all the characters

#

and not %s

winged inlet
#

Yeah, thats true

#

So yeah, just use std::substr and std::cout 😉

jovial crescent
winged inlet
jovial crescent
#

optimizations

#

performance diff

winged inlet
jovial crescent
#

and yes std::wcout<<std::wstring(wchar_ptr, wchar_len)<<L"\n" works just fine

#
std::wcout.write(arrRef, 1)<<L"\n";

this thing works thank god

#

it prints

#

A

#

maybe I should wipe all of the printf nonsense away from my program (copied from MSDN meant to be used in C) and use wcout

#

but still this doesn't explain why wprintf is broken

jovial crescent
#

I am using minGW g++

spare wren
jovial crescent
#

bump