This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Using fwprintf

Hi!

I am using 2-byte wide char strings in my application. Strings should be written and read from file (in flash FS). When using fwprintf(), i got in file only one byte per character, like converting UTF-8 to plain ASCII.

Here's my simple example:

int write_setup(DEVICE *bt_s)
{

    FILE *fout;

    fout = fopen (INI_FILE,"w");
    if (fout != NULL)
    {
                fwprintf (fout, L"NAME=%ls\n", bt_s->name);
        fclose (fout);
                return 0;
    }
        else return -1;
}

When I call this function, the bt_s->name points to memory with following contents:

0x54 0x00 0x65 0x00 0x73 0x00 0x74 0x00
or in ascii:
T<0>e<0>s<0>t<0>

when I check the file contents (with file hex editor), the ontents is plain ascii, one byte per character:
0x54 0x65 0x73 0x74, which is "Test"

Is this compiler related issue on using wide characters? What is proper use on file-wirting (and reading) wchar_t strings ?

Thanks!

Marko

  • Wide characters are converted to multibyte representation when written to a file. This requires selecting proper locale which can be done at link time or at runtime.

    The easiest way is to select the UTF-8 multibyte encoding by adding the following into your code:

    #pragma import(__use_utf8_ctype)
    

  • I made some further tests. I put some simple wchars:

    // 0100;LATIN CAPITAL LETTER A WITH MACRON;Lu;0;L;0041 0304;;;;N;LATIN CAPITAL LETTER A MACRON;;;0101;
                    fputwc(0x0100, fout);
    
    //03B1;GREEK SMALL LETTER ALPHA;Ll;0;L;;;;;N;;;0391;;0391
                    fputwc(0x03b1, fout);
    

    But those two chars are not in file at all. The char is written to file as one-byte only when wchar is < 0x00ff.

    And...the function

    fwide (fout, 1);
    


    returns 1.

    I really don't know what I am doing wrong. I read infocenter.arm.com/.../DUI0349A_rvct_libraries_guide.pdf and some other manuals.

    Marko