-
August 6th, 2010, 03:35 AM
#1
sprintf failing on %S ?
Code:
char szBuf[1024];
wchar_t wsz[] = L"-\x4e00-";
sprintf_s(szBuf, "abc %S def", wsz);
Why is this failing in an ANSI build ?
sprintf_s is returning -1, szBuf is set to an empty string.
I can totally understand that it can't convert the chinese character in there to the ANSI set and that it would either print nothing or a placeholder character in that spot, but why does it fail?
Is it supposed to fail under this condition?
Is there a way to work around it so it does at least give me either "abc -- def" or "abc -x- def" (with x being whatever placeholder char to signify no matching ANSI char)
-
August 6th, 2010, 04:06 AM
#2
Re: sprintf failing on %S ?
Originally Posted by OReubens
Code:
char szBuf[1024];
wchar_t wsz[] = L"-\x4e00-";
sprintf_s(szBuf, "abc %S def", wsz);
Why is this failing in an ANSI build ?
sprintf_s is returning -1, szBuf is set to an empty string.
I can totally understand that it can't convert the chinese character in there to the ANSI set and that it would either print nothing or a placeholder character in that spot, but why does it fail?
Debug into it and see what sprintf_s is doing.
Regards,
Paul McKenzie
-
August 6th, 2010, 05:17 AM
#3
Re: sprintf failing on %S ?
It could be that one of the security enhancements is in the way.
-
August 6th, 2010, 05:30 AM
#4
Re: sprintf failing on %S ?
Good point, looks like the call doesn't include the length of the output buffer, so it's strange that it compiles.
-
August 6th, 2010, 06:43 AM
#5
Re: sprintf failing on %S ?
Sorry, I see now why it compiles, I didn't use the template version before.
-
August 9th, 2010, 07:31 AM
#6
Re: sprintf failing on %S ?
I checked how the MSVC library handles this, and can't see a way to avoid this behaviour (unless you write your own sprintf.)
If the "C" locale is set, any two byte character above 255 will cause sprintf to fail with no ouptput and errno = EILSEQ (A wide-character code that does not correspond to a valid character has been detected.)
Using another locale, MSVC will call WideCharToMultiByte to convert to the system codepage. If this fails, or if the default character is used, sprintf will fail with no output and errno = EILSEQ.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|