Jump to ID:
QNX Operating System

Project Home

Documents

Discussions

Wiki

Project Info
Forum Topic - sprintf stops working after the German umlauts ä and ö, whereas ü does work: (7 Items)
   
 
 
sprintf stops working after the German umlauts ä and ö, whereas ü does work  
Some of the German umlauts are breaking the snprintf (and sprintf) processing.

Here is the affected code:

#include <cstring>
#include <iostream>
using namespace std;

int main(int argc, char *argv[])
{
    char strTmp[75];
    cout << "cout directly - " << "Umlauts: üäöÄÖÜ" << " - strlen: " << strlen("Umlauts: üäöÄÖÜ") << endl;
    strncpy(strTmp,  "Umlauts: üäöÄÖÜ", sizeof(strTmp)); // works
    cout << "strncpy       - " << strTmp << " - strlen: " << strlen(strTmp) << endl;  
    snprintf(strTmp, sizeof(strTmp), "Umlauts: üäöÄÖÜ"); 
    cout << "snprintf      - " << strTmp << " - strlen: " << strlen(strTmp) << endl; // shows only the first of the 
umlauts (ue)
    sprintf(strTmp, "Umlauts: üäöÄÖÜ");
    cout << "sprintf       - " << strTmp << " - strlen: " << strlen(strTmp) << endl; // shows only the first of the 
umlauts (ue)
}        


Each line should show all of the six German umlauts, but this is the result:

$ ./test
cout directly - Umlauts: üäöÄÖÜ - strlen: 15
strncpy       - Umlauts: üäöÄÖÜ - strlen: 15
snprintf      - Umlauts: ü - strlen: 10
sprintf       - Umlauts: ü - strlen: 10


The Momentics workspace is using the Windows cp1252 code page. Momentics is 6.3.0 SP3 on Windows XP SP2. gcc 3.3.5 is 
used:

C:/programme/QNX630/host/win32/x86/usr/bin/qcc -V3.3.5,gcc_ntox86 -c -Wc,-Wall -Wc,-Wno-parentheses  -O -Y_cpp -fmessage
-length=0 -lang-c++         main.cpp

C:/programme/QNX630/host/win32/x86/usr/bin/qcc -V3.3.5,gcc_ntox86  -lang-c++ -lang-c++ -Y_cpp    -otest    main.o  -L. -
LC:/programme/QNX630/target/qnx6/x86/lib/gcc/3.3.5 -LC:/programme/QNX630/target/qnx6/x86/lib -LC:/programme/QNX630/
target/qnx6/x86/usr/lib

Why is the string truncated after the German ü (ue)?

TiA
Christian


Re: sprintf stops working after the German umlautsä and ö, whereasü does work  
On Thu, 2008-04-17 at 06:40 -0400, Christian Leutloff wrote:
> Some of the German umlauts are breaking the snprintf (and sprintf)
> processing.
> 
> Here is the affected code:
> 
> #include <cstring> 
> #include <iostream> 
> using namespace std;
> 
> int main(int argc, char *argv[]) 
> { 
>     char strTmp[75]; 
>     cout << "cout directly - " << "Umlauts: üäöÄÖÜ" << " - strlen: "
> << strlen("Umlauts: üäöÄÖÜ") << endl; 
>     strncpy(strTmp,  "Umlauts: üäöÄÖÜ", sizeof(strTmp)); // works 
>     cout << "strncpy       - " << strTmp << " - strlen: " <<
> strlen(strTmp) << endl;  
>     snprintf(strTmp, sizeof(strTmp), "Umlauts: üäöÄÖÜ"); 
>     cout << "snprintf      - " << strTmp << " - strlen: " <<
> strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
> 
>     sprintf(strTmp, "Umlauts: üäöÄÖÜ"); 
>     cout << "sprintf       - " << strTmp << " - strlen: " <<
> strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
> 
> } 

Can you send a binary file of the program?

Just want to avoid any issue with unicode (when I cut and paste your
e-mail, I end up with the umlauts are in their unicode form).

Also, you could do:
{char *p=strTmp; (while(*p) { printf(" 0x%02x:%c",(unsigned)(*p), *p); p++;}

Just so we know exactlay what every function, or iostream is getting.

AW: sprintf stops working after the German umlautsä and ö, whereasü does work  
I've seen problems with non-7-bit characters occuring 
when the locale wasn't set correctly.

It's getting a little late over here, so I may be mistaken, 
but I think calling 
  setlocale( "C-TRADITIONAL" );
did help me in such cases.

- Thomas

> -----Ursprüngliche Nachricht-----
> Von: Stephane Boucher 
> Gesendet: 17 April 2008 17:03
> An: osmeta-core_os
> Betreff: Re: sprintf stops working after the German umlautsä and ö,
> whereasü does work
> 
> 
> On Thu, 2008-04-17 at 06:40 -0400, Christian Leutloff wrote:
> > Some of the German umlauts are breaking the snprintf (and sprintf)
> > processing.
> > 
> > Here is the affected code:
> > 
> > #include <cstring> 
> > #include <iostream> 
> > using namespace std;
> > 
> > int main(int argc, char *argv[]) 
> > { 
> >     char strTmp[75]; 
> >     cout << "cout directly - " << "Umlauts: üäöÄÖÜ" << " - strlen: "
> > << strlen("Umlauts: üäöÄÖÜ") << endl; 
> >     strncpy(strTmp,  "Umlauts: üäöÄÖÜ", sizeof(strTmp)); // works 
> >     cout << "strncpy       - " << strTmp << " - strlen: " <<
> > strlen(strTmp) << endl;  
> >     snprintf(strTmp, sizeof(strTmp), "Umlauts: üäöÄÖÜ"); 
> >     cout << "snprintf      - " << strTmp << " - strlen: " <<
> > strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
> > 
> >     sprintf(strTmp, "Umlauts: üäöÄÖÜ"); 
> >     cout << "sprintf       - " << strTmp << " - strlen: " <<
> > strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
> > 
> > } 
> 
> Can you send a binary file of the program?
> 
> Just want to avoid any issue with unicode (when I cut and paste your
> e-mail, I end up with the umlauts are in their unicode form).
> 
> Also, you could do:
> {char *p=strTmp; (while(*p) { printf(" 
> 0x%02x:%c",(unsigned)(*p), *p); p++;}
> 
> Just so we know exactlay what every function, or iostream is getting.
> 
> 
> 
> _______________________________________________
> OSMeta
> http://community.qnx.com/sf/go/post6985
> 
Re: AW: sprintf stops working after the German umlautsä and ö, whereasü does work  
I just tried the test case on a 6.4.0 system and they all printed out.
My first thought was the locale though...

Thomas Haupt wrote:
> I've seen problems with non-7-bit characters occuring
> when the locale wasn't set correctly.
> 
> It's getting a little late over here, so I may be mistaken,
> but I think calling
>   setlocale( "C-TRADITIONAL" );
> did help me in such cases.
> 
> - Thomas
> 
>  > -----Ursprüngliche Nachricht-----
>  > Von: Stephane Boucher
>  > Gesendet: 17 April 2008 17:03
>  > An: osmeta-core_os
>  > Betreff: Re: sprintf stops working after the German umlautsä and ö,
>  > whereasü does work
>  >
>  >
>  > On Thu, 2008-04-17 at 06:40 -0400, Christian Leutloff wrote:
>  > > Some of the German umlauts are breaking the snprintf (and sprintf)
>  > > processing.
>  > >
>  > > Here is the affected code:
>  > >
>  > > #include <cstring>
>  > > #include <iostream>
>  > > using namespace std;
>  > >
>  > > int main(int argc, char *argv[])
>  > > {
>  > >     char strTmp[75];
>  > >     cout << "cout directly - " << "Umlauts: üäöÄÖÜ" << " - strlen: "
>  > > << strlen("Umlauts: üäöÄÖÜ") << endl;
>  > >     strncpy(strTmp,  "Umlauts: üäöÄÖÜ", sizeof(strTmp)); // works
>  > >     cout << "strncpy       - " << strTmp << " - strlen: " <<
>  > > strlen(strTmp) << endl; 
>  > >     snprintf(strTmp, sizeof(strTmp), "Umlauts: üäöÄÖÜ");
>  > >     cout << "snprintf      - " << strTmp << " - strlen: " <<
>  > > strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
>  > >
>  > >     sprintf(strTmp, "Umlauts: üäöÄÖÜ");
>  > >     cout << "sprintf       - " << strTmp << " - strlen: " <<
>  > > strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
>  > >
>  > > }
>  >
>  > Can you send a binary file of the program?
>  >
>  > Just want to avoid any issue with unicode (when I cut and paste your
>  > e-mail, I end up with the umlauts are in their unicode form).
>  >
>  > Also, you could do:
>  > {char *p=strTmp; (while(*p) { printf("
>  > 0x%02x:%c",(unsigned)(*p), *p); p++;}
>  >
>  > Just so we know exactlay what every function, or iostream is getting.
>  >
>  >
>  >
>  > _______________________________________________
>  > OSMeta
>  > http://community.qnx.com/sf/go/post6985
>  >
> 
> _______________________________________________
> OSMeta
> http://community.qnx.com/sf/go/post6987
> 

-- 
cburgess@qnx.com
Re: AW: sprintf stops working after the Germanumlautsä and ö, whereasü does work  
On Thu, 2008-04-17 at 11:13 -0400, Colin Burgess wrote:
> I just tried the test case on a 6.4.0 system and they all printed
> out. 
> My first thought was the locale though...

Well, me too, but I got a strlen of 21.  The textual lenght is 15, but
strlen() of the unicode version is 21 :-) (hence my cut and paste gave
me unicode strings)

Re: AW: sprintf stops working after the German umlauts ä and ö, whereas ü does work  
> I just tried the test case on a 6.4.0 system and they all printed out.

That are good news.

The fix to set the locale before using snprintf leads to another problem when using Photon 8-(

When sending the above string, it is required to convert the characters to the utf-8 encoding Photon uses. This will 
fail (string is truncated) when locale is set to C-TRADITIONAL.

The PxTranslateStateToUTF does work again, when I switch the locale back to "C".

My solution for this problem will be to drop snprintf and use stringstream instead. Therefore there is no need to set 
the locale for me.
Re: AW: sprintf stops working after the German umlautsä and ö, whereasü does work  
> I've seen problems with non-7-bit characters occuring 
> when the locale wasn't set correctly.
> 
> It's getting a little late over here, so I may be mistaken, 
> but I think calling 
>   setlocale( "C-TRADITIONAL" );
> did help me in such cases.
> 
> - Thomas
> 
> > -----Ursprüngliche Nachricht-----
> > Von: Stephane Boucher 
> > Gesendet: 17 April 2008 17:03
> > An: osmeta-core_os
> > Betreff: Re: sprintf stops working after the German umlautsä and ö,
> > whereasü does work
> > 
> > 
> > On Thu, 2008-04-17 at 06:40 -0400, Christian Leutloff wrote:
> > > Some of the German umlauts are breaking the snprintf (and sprintf)
> > > processing.
> > > 
> > > Here is the affected code:
> > > 
> > > #include <cstring> 
> > > #include <iostream> 
> > > using namespace std;
> > > 
> > > int main(int argc, char *argv[]) 
> > > { 
> > >     char strTmp[75]; 
> > >     cout << "cout directly - " << "Umlauts: üäöÄÖÜ" << " - strlen: "
> > > << strlen("Umlauts: üäöÄÖÜ") << endl; 
> > >     strncpy(strTmp,  "Umlauts: üäöÄÖÜ", sizeof(strTmp
> )); // works 
> > >     cout << "strncpy       - " << strTmp << " - strlen: " <<
> > > strlen(strTmp) << endl;  
> > >     snprintf(strTmp, sizeof(strTmp), "
> Umlauts: üäöÄÖÜ"); 
> > >     cout << "snprintf      - " << strTmp << " - strlen: " <<
> > > strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
> > > 
> > >     sprintf(strTmp, "Umlauts: üäöÄÖÜ"); 
> > >     cout << "sprintf       - " << strTmp << " - strlen: " <<
> > > strlen(strTmp) << endl; // shows only the first of the umlauts (ue)
> > > 
> > > } 
> > 
> > Can you send a binary file of the program?
> > 
> > Just want to avoid any issue with unicode (when I cut and paste your
> > e-mail, I end up with the umlauts are in their unicode form).
> > 
> > Also, you could do:
> > {char *p=strTmp; (while(*p) { printf(" 
> > 0x%02x:%c",(unsigned)(*p), *p); p++;}
> > 
> > Just so we know exactlay what every function, or iostream is getting.

the while/printf leads to:

 0x55:U 0x6d:m 0x6c:l 0x61:a 0x75:u 0x74:t 0x73:s 0x3a:: 0x20:  0xfffffffc:ü
 

After setting 
(void)setlocale(LC_CTYPE, "C-TRADITIONAL");

Everything work as expected:

cout directly - Umlauts: üäöÄÖÜ - strlen: 15
strncpy       - Umlauts: üäöÄÖÜ - strlen: 15
snprintf      - Umlauts: üäöÄÖÜ - strlen: 15
sprintf       - Umlauts: üäöÄÖÜ - strlen: 15
 0x55:U 0x6d:m 0x6c:l 0x61:a 0x75:u 0x74:t 0x73:s 0x3a:: 0x20:  0xfffffffc:ü 0xffffffe4:ä 0xfffffff6:ö 0xffffffc4:Ä 
0xffffffd6:Ö 0xffffffdc:Ü

Thanks a lot for the quick and very helpful answers!

Christian