_wfopen and extra spaces

This is a discussion on _wfopen and extra spaces within the C++ Programming forums, part of the General Programming Boards category; Im trying to write to a file with unicode but when i use _wfopen i get extra spaces between the ...

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

    _wfopen and extra spaces

    Im trying to write to a file with unicode but when i use _wfopen i get extra spaces between the letters like
    H e l l o
    instead of
    Hello.
    I cant figure out why.

    Code:
    #include <stdio.h>
    #include <stddef.h>
    #include <stdlib.h>
    #include <wchar.h>
    
    #define BUFFER_SIZE 50
    
    int main(int argc, char** argv)
    {
        wchar_t str[BUFFER_SIZE];
        size_t  strSize;
        FILE*   fileHandle;
    
        if ((fileHandle = _wfopen( L"_wfopen_test.txt",L"wt+,ccs=UNICODE")) == NULL)
        {
            wprintf(L"_wfopen failed!\n");
            return(0);
        }
        wcscpy_s(str, sizeof(str)/sizeof(wchar_t), L"Hello\n");
        strSize = wcslen(str);
        if (fwrite(str, sizeof(wchar_t), strSize, fileHandle) != strSize)
        {
            wprintf(L"fwrite failed!\n");
        }
        if (fclose(fileHandle))
        {
            wprintf(L"fclose failed!\n");
        }
        return 0;
    }
    Compiler MSVC++ 2013 with Code::Blocks.

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    What are you using to VIEW the file? If the viewer doesn't understand Unicode, it will display like this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  3. #3
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,669
    When you use "css=UNICODE", and the existing file has no BOM or the file is new, you get ANSI encoding when you use formatted output.

    The next problem is that you are not using formatted output. fwrite() is unformatted output. It writes exactly what you tell it to (with the exception of text mode translations of \n to \r\n).

    fputws(L"Hello\n", fileHandle);

    gg

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    795
    @brewbuck
    I was using notepad and wordpad.
    Thanks.

    @Codeplug
    Thank you for the explanation, its working with fputws.
    Compiler MSVC++ 2013 with Code::Blocks.

Popular pages Recent additions subscribe to a feed

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