Ogre读取中文路径名的文件失败的解决办法

本文介绍了解决Ogre引擎在VS2005环境下读取中文路径文件的问题,通过修改OgreFileSystem.cpp文件中的FileSystemArchive::open方法实现。此外,还提供了一种使用setlocale设置本地化的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ogre的文件读取是使用的标准库的io库读取的,众所周知的是,在vs2005是存在着bug的。
因此想要一劳永逸的解决这个办法唯有去修改Ogre的源代码,以下为修改方法:

打开OgreFileSystem.cpp文件,找到FileSystemArchive::open方法,使用以下代码替换之:
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
        static std::vector<wchar_t>    s_wchar_buf((size_t)128);
        size_t lengthUnicode 
= MultiByteToWideChar(CP_ACP, 0, full_path.c_str(), full_path.size(), NULL, 0);
        
if (s_wchar_buf.size() < lengthUnicode + 1)
        
{
            s_wchar_buf.resize(lengthUnicode 
* 2);
        }

        wchar_t
* szUnicode = &s_wchar_buf[0];
        MultiByteToWideChar(CP_ACP, 
0, full_path.c_str(), full_path.size(), szUnicode, lengthUnicode);
        szUnicode[lengthUnicode] 
= 0;
        std::ifstream
* origStream = new std::ifstream();
        origStream
->open(szUnicode, std::ios::in | std::ios::binary);

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


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

posted on 2008-12-09 03:42 杨粼波 阅读(957) 评论(2)   编辑  收藏 引用

Feedback

 re: Ogre读取中文路径名的文件失败的解决办法 2010-09-05 19:16
用setlocale(LC_ALL,"Chinese-simplified");就可以了。    
  

 re: Ogre读取中文路径名的文件失败的解决办法[未登录] 2010-09-12 00:56
你知道setlocale这个方法的代价有多大么?会带来什么影响么?    
  

转载于:https://www.cnblogs.com/lancidie/archive/2011/04/23/2025572.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值