CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2013
    Location
    Prague / Cracow
    Posts
    47

    CreateFile not creating UNICODE file

    Hey ho!

    I wanna create an XML file which will be in UNICODE format, otherwise EXCEL won't read the local diacritic chars (e.g "ń", "ś", "ć" etc). The chars are in the file after generation but due to some reason EXCEL does not understand it as a compatible XML format. I always need to open the file in notepad manually and change the coding from ANSI to UNICODE (just save as UNICODE) and only then i can open it in EXCEL.

    Is there any way of doing this already when the file is created in my program ?
    I use this:
    Code:
    HANDLE plik = CreateFile( filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL );
    I tried CreateFileW as well as e.g. _T(filename) but the file is always generated as ANSI...

    any idea?
    would be grateful for any hint.

    thankx
    berkov
    .

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: CreateFile not creating UNICODE file

    Have you compiled the program as a unicode program?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2013
    Location
    Prague / Cracow
    Posts
    47

    Re: CreateFile not creating UNICODE file

    Nope, it's "Use Multi-Byte Character Set"
    if i change it to UNICODE then all string (and i've got hundreds of them) are red underlined by compiler and the compiler won't let me build.
    i am not good at all those conversions but i guess i would have to use _T() with each string if i decide to go for project in UNICODE.

    Isn't there any way to just set the property of newly generated file to "UNICODE"?

    b
    .

  4. #4
    Join Date
    Nov 2003
    Posts
    1,902

    Re: CreateFile not creating UNICODE file

    You are in complete control of the contents of the file. The WriteFile() API deals with bytes. The bytes you tell it to write is what is written.

    On Windows when a file is "saved as Unicode" that typically means that a BOM is added to the beginning of the file. This tells readers of the file how the file is encoded.

    gg

  5. #5
    Join Date
    Apr 2013
    Location
    Prague / Cracow
    Posts
    47

    Re: CreateFile not creating UNICODE file

    @Codeplug, ok but then apparently EXCEL wants that "BOM" at the beginning in order to interpret diacritics in the file correctly.
    If i create a file using my program and there are some diacritics inside then excel does not understand it.
    it's enough to open it in notepad and (not changing the content itself) save as the UNICODE - excel will understand it.
    But i wanna be able to create an excel compatible file directly in my program.
    Last edited by berkov; May 29th, 2013 at 05:34 PM.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: CreateFile not creating UNICODE file

    >> If i create a file using my program and there are some diacritics inside ...
    First you need to know how these characters are encoded. Your local ansi codepage (ACP), UTF8, UTF16? If you don't know, describe where the characters come from.

    >> But i wanna be able to create an excel compatible file directly ...
    So write the BOM at the beginning of the file, then write the file in the encoding indicated by the BOM.

    Or you may be able to just set the proper encoding attribute and not worry about having to covert to Unicode.

    gg

  7. #7
    Join Date
    Apr 2013
    Location
    Prague / Cracow
    Posts
    47

    Re: CreateFile not creating UNICODE file

    hi.. the whole thing with encoding is so messy! :-)
    and i need to admit i don't really get it.


    so first question, where do i take the chars from?
    3 different sources:
    - 1st part of the xml comes from another file (template) saved in ANSI
    - 2nd from the _bstr_t string entered in the code itself (VS Express in MultiByte mode)
    - 3rd from ADO (Access) database also from _bstr_t string
    What i tried is to change the 1st part (external ANSI template file) to UNICODE. and run the application but then the WHOLE file generated via my application was in Chinese LOL "‼圼牯獫敨瑥猠㩳慎敭✽敒潰瑲✱‾਍吼扡敬猠㩳硅慰摮摥"

    Second question:
    i will read that wiki article in more details at work today but what i tired so far is the solution from w3schools.
    if i add one of those 3:
    <?xml version="1.0" encoding="us-ascii"?>
    <?xml version="1.0" encoding="windows-1252"?>
    <?xml version="1.0" encoding="ISO-8859-1"?>
    excel understands the file and finally opens it, however in any of those my diacritic chars are shown correctly. it's always something like "ê¹ ³¹¿" but again, if I edit the file in notepad and change it to UNICODE the chars in excel are shown correctly (regardless of the encoding header entered into xml itself).
    So your solution is good coz the file is "open-able" in excel but if i could have the local diacritics shown correctly within the cells, i would be just more than happy!
    Last edited by berkov; May 30th, 2013 at 06:19 AM.

  8. #8
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: CreateFile not creating UNICODE file

    Don't create an XML by using createfile and doing it all yourself. There's a LOT of details, and it can be very cumbersome if you have to deal with namespaces.

    Use a proper XML library/class.

  9. #9
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: CreateFile not creating UNICODE file

    Quote Originally Posted by berkov View Post
    I tried CreateFileW as well as e.g. _T(filename) but the file is always generated as ANSI...
    CreateFile just creates file on file system. What content you're going to put there is totally up to you. You may create file with CreateFileA but still be able to put Unicode content there. Unrelated things, absolutely.
    Best regards,
    Igor

  10. #10
    Join Date
    Nov 2003
    Posts
    1,902

    Re: CreateFile not creating UNICODE file

    Most of your content comes form _bstr_t strings, which are encoded as "UTF-16LE". Since the majority is already in a Unicode encoding, it's best to keep it in a Unicode encoding so that nothing is lost due to Unicode<->Codepage conversions.

    I would recommend using encoding="UTF-8" in your XML template. If your template doesn't have special characters in it, then that's all you'll need to do to the template.

    The next step is converting your _bstr_t strings to UTF8 before writing it to the file. You can use the code in this thread to do that: http://forums.codeguru.com/showthrea...82#post1877082
    The usage would be something like this:
    Code:
    _bstr_t bstr;
    std::string utf8str = wstr_to_str((const whcar_t*)bstr, CP_UTF8);
    const char *raw = utf8str.c_str();
    gg

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