提高::文件系统和统一$ Linux和Windows在c $ C [英] boost::filesystem and Unicode under Linux and Windows

查看:146
本文介绍了提高::文件系统和统一$ Linux和Windows在c $ C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下程序在Visual Studio 2008编译Windows下,都与字符集
采用UNI code字符集和使用多字节字符集。然而,这并不Ubuntu下10.04.2 LTS 64位和GCC 4.4.3编译。我使用升压1.46.1两种环境下。

The following program compiles in Visual Studio 2008 under Windows, both with Character Set "Use Unicode Character Set" and "Use Multi-Byte Character Set". However, it does not compile under Ubuntu 10.04.2 LTS 64-bit and GCC 4.4.3. I use Boost 1.46.1 under both environments.

#include <boost/filesystem/path.hpp>
#include <iostream>

int main() {
  boost::filesystem::path p(L"/test/test2");
  std::wcout << p.native() << std::endl;
  return 0;
}

Linux下的编译错误是:

The compile error under Linux is:

TEST.CPP:6:错误:不对应的运营商的LT;&LT;'中'的std :: wcout&LT;&LT; p.boost :: filesystem3 ::路径::本地()

test.cpp:6: error: no match for ‘operator<<’ in ‘std::wcout << p.boost::filesystem3::path::native()’

在我看来像的boost :: Linux下的文件系统并不能提供路径)宽字符的字符串::本机(尽管已用一个宽字符串初始化的boost ::文件系统::路径。此外,我猜,这是因为Linux默认为UTF-8和Windows为UTF-16。

It looks to me like boost::filesystem under Linux does not provide a wide character string in path::native(), despite boost::filesystem::path having been initialized with a wide string. Further, I'm guessing that this is because Linux defaults to UTF-8 and Windows to UTF-16.

所以我的第一个问题是,我怎么写这两个平台上使用的boost ::文件系统,并支持统一code路径的程序?

So my first question is, how do I write a program that uses boost::filesystem and supports Unicode paths on both platforms?

第二个问题:当我运行在Windows下这个程序,它输出:

Second question: When I run this program under Windows, it outputs:

/test/test2

我的理解是,本机()方法应该将路径转换到Windows下的原生格式,这是使用反斜杠而不是正斜杠。为什么串出来POSIX格式?

My understanding is that the native() method should convert the path to the native format under Windows, which is using backslashes instead of forward slashes. Why is the string coming out in POSIX format?

推荐答案

如果您要使用的宽输出流,你要转换为宽字符串:

If you want to use the wide output streams, you have to convert to a wide string:

#include <boost/filesystem/path.hpp>
#include <iostream>

int main() {
  boost::filesystem::path p(L"/test/test2");
  std::wcout << p.wstring() << std::endl;
  return 0;
}

使用注意AFAIK wcout 不给你在Windows上的Uni code输出;你需要使用 wprintf 代替。

Note that AFAIK using wcout doesn't give you Unicode output on Windows; you need to use wprintf instead.

这篇关于提高::文件系统和统一$ Linux和Windows在c $ C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆