Unicode with garbage chars

This is a discussion on Unicode with garbage chars within the C Programming forums, part of the General Programming Boards category; I get garbage characters after "Hello" either i print it to the console or write it to a file. How ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747

    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;
    }
    Compiler MSVC++ 2010 with Code::Blocks.

  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
    Location
    France
    Posts
    747
    You dont need because it's unicode.
    Compiler MSVC++ 2010 with Code::Blocks.

  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
    Location
    France
    Posts
    747
    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.
    Compiler MSVC++ 2010 with Code::Blocks.

  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
    Location
    France
    Posts
    747
    Wow, awesome, so simple!

    And i was searching high and low...

    Thank you very much Bayint Naung!
    Compiler MSVC++ 2010 with Code::Blocks.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,184
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  9. #9
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    747
    Thank you Elysia!
    Compiler MSVC++ 2010 with Code::Blocks.

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21