C++如何支持中文路径

‘在写程序的时候,发现当含有中文路径的时候,文档读取便会失败。

我使用的路径转化代码如下,将路径中所有的”\“ 转换为"\\“。

CString transFile2Path(CString cs)
{
	CString re=cs;
	int srt_pos = 0;
	int find_pos = -1;
	while(1)
	{
		find_pos =re.Find('\\',srt_pos);
		if(find_pos ==-1)
		{
			return re;
		}
		re.Insert(find_pos,'\\');
		srt_pos=find_pos+2;
	}
	return re;
}

网上有说:

以下示例如何用宽字符API处理中文路径和文件名

#include <io.h>
#include <stdio.h>
 
void process_directory(const wchar_t * filespec) // 分析目录,遍历所有满足条件的文件
{
    struct _wfinddata_t fileinfo;
    intptr_t handle;
 
    if ((handle=_wfindfirst(filespec, &fileinfo)) == -1L)
    {
        perror("Files open error");
    }
    else
    {
        do
        {
            // 处理文件
        } while (_wfindnext(handle, &fileinfo) == 0);
        _findclose(handle);
    }
}
 
int main()
{
    process_directory(L"C:\\数据\\*.*");
    return 0;
}

http://bbs.csdn.net/topics/330118362


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值