Thread: Unicode with garbage chars

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Unicode with garbage chars

    I get garbage characters after "Hello" either i print it to the console or write it to a file.
    How to print out only "Hello" ?

    Code:
    int main(void)
    {
      
        WCHAR* buf3=L"Hello world!",buf4[32];
      
        wcsncpy(buf4, buf3,6);
        wprintf (L"%s",buf4);
        FILE * file = _wfopen(L"uni", L"w");
        fwrite(buf4, sizeof(wchar_t), wcslen( buf4 ), file);
      
        free(buf3);
    
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Is it null ('\0') terminated?

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    You dont need because it's unicode.
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Are you sure?
    Don't you need to use %ls ?
    Last edited by Bayint Naung; 07-07-2010 at 06:44 AM.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Ok you got me, i had to.

    I wasnt sure because in another program it worked without zeroing the end.

    This is how i did it by the way.

    Code:
    buf4[6] = 0x000D;
    buf4[7] = 0x000A;
    buf4[8] = 0x0000;
    Is there a simpler way to do it in Unicode?

    Ps yes i need %ls thank you!
    Last edited by Ducky; 07-07-2010 at 06:58 AM.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    A simple test:
    Code:
    if( buf4[8] == L'\0' ) {
     printf("YES!!! I'm null terminated by luck!\n");
    }

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Wow, awesome, so simple!

    And i was searching high and low...

    Thank you very much Bayint Naung!
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Ducky View Post
    Ok you got me, i had to.

    I wasnt sure because in another program it worked without zeroing the end.

    This is how i did it by the way.

    Code:
    buf4[6] = 0x000D;
    buf4[7] = 0x000A;
    buf4[8] = 0x0000;
    Is there a simpler way to do it in Unicode?

    Ps yes i need %ls thank you!
    Umm...
    Code:
    wcscpy(&buf4[6], L"\r\n");
    ?

    Or if you really want
    Code:
    buf4[6] = L'\r';
    buf4[7] = L'\n';
    buf4[8] = L'\0';
    Quote Originally Posted by Ducky View Post
    You dont need because it's unicode.
    How do you propose wprintf is able to know where the string ends, then?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Thank you Elysia!
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  2. Unicode - a lot of confusion...
    By Jumper in forum Windows Programming
    Replies: 11
    Last Post: 07-05-2004, 07:59 AM
  3. Should I go to unicode?
    By nickname_changed in forum C++ Programming
    Replies: 10
    Last Post: 10-13-2003, 11:37 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM
  5. fancy strcpy
    By heat511 in forum C++ Programming
    Replies: 34
    Last Post: 05-01-2002, 04:29 PM