您的位置:首页 > 编程语言 > C语言/C++

C++如何支持中文路径

2015-02-07 17:24 281 查看
‘在写程序的时候,发现当含有中文路径的时候,文档读取便会失败。

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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: