CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2001
    Location
    San Diego
    Posts
    102

    Need Help in Understanding HOWTO Write UNICODE String to a File

    How do I write to a file using UNICODE. I have searched the archives with no real answers. What do I need to do first, second and third.

    I know how to use var.WriteString("whatever");

    What are the parameters do I use to open a file to write?
    Do I do it this way:
    Code:
    sConfFile.Open((CString)m_sConfigFile,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);
    or is there another way.

    I am still learning this VC++ stuff so I am new to a lot of things.

    Thanks in advance

    Mike@spb

  2. #2
    Join Date
    Nov 2001
    Location
    France
    Posts
    178
    I don't know much either about Unicode, but it seems that you should use the _T prefix for your string ...

  3. #3
    Join Date
    Nov 2001
    Location
    France
    Posts
    178

  4. #4
    Join Date
    Feb 2003
    Location
    California
    Posts
    334
    You need to decide how to store your Unicode string. You can store it in UTF-16, or double byte. In this case, you can either use wide versions of the file writer methods, or write your strings out as raw data. (Your data length would be [wcslen(str) * sizeof(WCHAR)]). You can also convert your strings to MBCS, in which case you can use the A2W macro from ATL, or manually use WideCharToMultibyte(). Personally, I prefer writing the data out in UTF-8.

    Obviously, you need to read your data back the same way you wrote it out.
    Henri Hein
    Principal Engineer, Propel
    Do not credit Propel with my views or opinions.

  5. #5
    Join Date
    Nov 2001
    Location
    San Diego
    Posts
    102
    OK, now I'm confused. When I try to open the original file the first line looks like this in unicode:
    Code:
     [][][][][]<[]p[]r[]o[]f[]i[]l[]e[][]v[]e[]r[]s[]i[]o[]n[]=[]"[]4[]5[]8[]7[]5[]2[]"[]
    The "brackets" are actually squares when I read the file using wordpad or word.

    When I do a test to try in duplicating this file it is written out as text. Here is what I have

    Code:
    #deine _UNICODE
    CFile sTest;
    CString sfile = "C:\\test.prx";
    
    sTest.Open(sfile, CFile::modeCreate|CFile::modeWrite);
    CString str = _T("\"is is some test\"");
    sTest.Write(str, (str.GetLength()+1) * sizeof(TCHAR));
    sTest.Close();
    The result of the output to the file is "This is some test"[] instead of []"[]T[]h[]i[]s[] []i[]s[] []s[]o[]m[]e[] []t[]e[]s[]t[]"[] .

    What am I doing wrong?

    Mike@spb

  6. #6
    Join Date
    Nov 2001
    Posts
    69
    Hi Mike,
    You are not doing anything wrong. What you see is correct and that IS unicode text. It is a 16-bit character. In usual case, the first 8 bit contains the ascii code for the character and the second 8 bits are 0. That's the reason why you see the [] symbol if your viewer program does not support unicode strings. My notepad is Win2K supports unicode strings and I can view the string properly.

    Hope this makes you clear!

  7. #7
    Join Date
    Nov 2001
    Location
    San Diego
    Posts
    102
    More questions. When I use the sample, as you describe as being correct. It seems to be in UNICODE; however, the application that reads this UNICODE file does not recognize it. If I save the file as a Unicode Text Document in Wordpad, the application then is able to see the unicode file. The applicaiton I am referring to is the Windows Media Encoder. The profiles it uses are in UNICODE text.

    If I am writing in UTF-16, maybe I should be writing it out in UTF-8. What do I need to change to write to UTF-8 so I can see if the Windows Media Encoder recognizes the profile written in UTF-8?

    Examples would help.

    Mike@spb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured