• ogre无法读取中文路径的解决办法


    下面是一段ogre中的文件操作相关的源码

    代码
        DataStreamPtr FileSystemArchive::open(const String& filename) const
        {
            String full_path 
    = concatenate_path(mName, filename);

            
    // Use filesystem to determine size 
            
    // (quicker than streaming to the end and back)
            struct stat tagStat;
        
    int ret = stat(full_path.c_str(), &tagStat);
            assert(ret 
    == 0 && "Problem getting file size" );

            
    // Always open in binary mode
            std::ifstream *origStream = OGRE_NEW_T(std::ifstream, MEMCATEGORY_GENERAL)();
            origStream
    ->open(full_path.c_str(), std::ios::in | std::ios::binary);

            
    // Should check ensure open succeeded, in case fail for some reason.
            if (origStream->fail())
            {
                OGRE_DELETE_T(origStream, basic_ifstream, MEMCATEGORY_GENERAL);
                OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND,
                    
    "Cannot open file: " + filename,
                    
    "FileSystemArchive::open");
            }

            
    /// Construct return stream, tell it to delete on destroy
            FileStreamDataStream* stream = OGRE_NEW FileStreamDataStream(filename,
                origStream, tagStat.st_size, 
    true);
            
    return DataStreamPtr(stream);
        }

    主要的代码是

    origStream->open(full_path.c_str(), std::ios::in | std::ios::binary);

    通过在文件流打开文件之前,我们设置一下语言环境

    std::locale::global(std::locale(""));

    接下来我们发现,中文路径的问题解决了,但是向文件中写入整型或浮点型数据时会有问题,比如“1000”,输出之后就成了“1,000”

    这正是因为我们改变了语言环境的原因,为了将修改减少到最小,我们应该在文件打开完毕后,恢复之前的设置

    std::locale saveLocal = std::locale::global(std::locale(""));
    origStream
    ->open(full_path.c_str(), std::ios::in | std::ios::binary);
    std::locale::
    global(saveLocal);

    everything is fine now~

  • 相关阅读:
    android architecture
    java求解两个字符串之间的编辑距离
    URL和URI的区别
    java源码解析之qdox
    java中Random实现原理
    java中ThreadLocal入门
    java中多线程之Future入门
    java中类加载器入门
    java中正则表达式的group用法
    java反射调用main方法踩坑
  • 原文地址:https://www.cnblogs.com/oiramario/p/1626304.html
Copyright © 2020-2023  润新知