Thread: _wfopen and extra spaces

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

    _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;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,981
    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
    932
    @brewbuck
    I was using notepad and wordpad.
    Thanks.

    @Codeplug
    Thank you for the explanation, its working with fputws.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed