Search code examples
utf-8non-ascii-characterstinyxml

TiXmlDocument::LoadFile for non-latin path names


I use tinyXml to store configuration file under FOLDERID_RoamingAppData, or in other words, under C:\Users\USERNAME\AppData\Roaming. I use the following sequence (simplified):

  1. SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &path) to get the path
  2. Encode path as UTF8
  3. Call m_doc.LoadFile(path) // m_doc is TiXmlDocument object
  4. call m_doc.Savefile() // This one fails if user name is non-latin

I wonder how I can LoadFile/SaveFile when the filename contains non-latin (e.g. Hebrew) characters.


Solution

  • There is no portable way to codify Windows filenames in char-based variables (C-strings or std::strings), since the Windows API does not accept the UTF8 encoding.

    Luckily, TinyXml offers an alternative form of LoadFile, accepting a FILE* as argument. You can use the Windows _wfopen to get such handle: here is a snippet:

    SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &path);
    FILE* xmlFile = _wfopen(path, L"r+");
    m_doc.LoadFile(xmlFile);
    

    The corresponding function SaveFile works in the same manner – in this case use L"w" as second parameter of the _wfopen call to overwrite the previous version of the file.