CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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)

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: sprintf failing on %S ?

    Quote Originally Posted by OReubens View Post
    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

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: sprintf failing on %S ?

    It could be that one of the security enhancements is in the way.

  4. #4
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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.

  5. #5
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    Re: sprintf failing on %S ?

    Sorry, I see now why it compiles, I didn't use the template version before.

  6. #6
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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
  •  





Click Here to Expand Forum to Full Width

Featured