iostream问题:如何打开unicode文件名。 [英] iostream question: How to open unicode file name.

查看:81
本文介绍了iostream问题:如何打开unicode文件名。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试升级使用旧iostream的旧代码。


在代码中的一个地方,我在wchar_t字符串中有一个路径/文件名

(unicode utf-16)。


我需要打开一个ifstream到该文件。但ifstream上的open()只有

需要char *字符串(mbcs?)。


在旧的iostream中,我可以_wopen()文件,得到filedesc,并在ifstream上调用

attach()。

但是标准的iostream中不存在filedescs和attach()。

我不能将unicode字符串转换为mbcs字符串,因为用户的

默认代码页可能不允许

unicode string。


我当然可以将unicode字符串转换为utf-8,但我找不到任何

方式让iostream open()相信字符串是utf-8。


尝试ibue()一个具有自定义代码的自定义语言环境的流

似乎只会影响写入流的数据,而不会影响文件名如何通过

来解释。


有谁知道我怎么能完成打开ifstream到名为

文件的unicode?


为什么ifstream没有wopen()?

解决方案

Charles F McDevitt写道:

我正在尝试升级一些使用旧iostream的旧代码。

代码中的一个地方,我在wchar_t字符串中有一个路径/文件名
(unicode utf-16)。

我需要打开一个ifstream到该文件。但ifstream上的open()只需要char *字符串(mbcs?)。

在旧的iostream中,我可以_wopen()文件,获取filedesc,和
在ifstream上调用attach()。
但是标准的iostream中不存在filedescs和attach()。

我不能将unicode字符串转换为mbcs字符串,因为
用户的默认代码页可能不允许
在unicode字符串中的所有字符。

我当然可以将unicode字符串转换为utf-8 ,但我无法找到
任何方式让iostream打开()相信字符串是
utf-8。

尝试ibue()一个流使用自定义区域设置有一个自定义的codecvt似乎只影响写入流的数据,而不是如何解释传递给open的文件名称。

有谁知道我是怎么做的可以完成打开一个ifstream到一个名为unicode的文件吗?

为什么ifstream都没有wopen()?




因为C ++社区不能就这样的函数如何工作达成一致。

对于具有Unicode文件系统的Windows系统,所需的

行为似乎很明显:只需将字符串传递给文件系统即可。但是,自从
C ++试图解决更广泛的系统,其中许多系统不支持unicode文件系统,我们留下了C ++标准哪个

没有标准兼容,可以通过unicode文件打开文件的便携方式

名称。


库实现随VC提供的确提供了一种方法,但是,b $ b。使用C运行时库_wfopen打开文件,然后通过传递_wfopen()返回到ifstream

构造函数的FILE *来创建

ifstream。


#include< fstream>


void foo(const wchar_t * pwsz)

{

std :: ifstream stm(_wfopen(pwsz,L" rb"));


//用流做的东西

}


-cd




" Carl Daniel [VC ++ MVP]" < CP ****** @ nospam.mvps.org>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...

Charles F McDevitt写道:

我正在尝试升级一些使用旧iostream的旧代码。

在代码的一个地方,我在wchar_t中有一个路径/文件名string
(unicode utf-16)。

我需要打开一个ifstream到该文件。但ifstream上的open()只需要char *字符串(mbcs?)。

在旧的iostream中,我可以_wopen()文件,获取filedesc,和
在ifstream上调用attach()。
但是标准的iostream中不存在filedescs和attach()。

我不能将unicode字符串转换为mbcs字符串,因为
用户的默认代码页可能不允许
在unicode字符串中的所有字符。

我当然可以将unicode字符串转换为utf-8 ,但我无法找到
任何方式让iostream打开()相信字符串是
utf-8。

尝试ibue()一个流使用自定义区域设置有一个自定义的codecvt似乎只影响写入流的数据,而不是如何解释传递给open的文件名称。

有谁知道我是怎么做的可以完成打开ifstream到一个名为unicode的文件吗?

为什么ifstream都没有wopen()?
因为C + +社区不能就这样的功能如何工作达成一致。
对于具有Unicode文件系统的Windows系统,所需的行为似乎很明显:只需将字符串传递给文件系统即可。但是



因为C ++试图解决范围更广的系统,其中许多系统都不支持unicode文件系统,我们留下了C ++标准。给定一个unicode文件名称的
是没有标准兼容,可移植的方式打开文件。

VC提供的库实现确实提供了一种方法,<但是。使用C运行时库_wfopen打开文件,然后通过传递_wfopen()返回到ifstream
构造函数的FILE *来创建
ifstream。

#include< fstream> ;

void foo(const wchar_t * pwsz)
{
std :: ifstream stm(_wfopen(pwsz,L" rb"));
//用流做的东西
}





谢谢..我想那个'这是我唯一的选择。


" Carl Daniel [VC ++ MVP]" < CP ****** @ nospam.mvps.org>写道:

[...]

为什么ifstream没有wopen()?
因为C ++社区不能就如何这样的功能应该可行。
对于具有Unicode文件系统的Windows系统,所需的行为似乎很明显:只需将字符串传递给文件系统即可。但是,由于C ++试图解决范围更广的系统,其中许多系统不支持unicode文件系统,我们留下了一个C ++标准,其中
不是给定unicode文件名称的标准兼容,可移植方式打开文件。




我认为这是一个弱论点。毕竟,

C ++也试图解决那些根本没有文件系统的系统。

(Carl,我认识你不是那个与

争论的人。但是,我只是不能让那个

通过未注释。< g>)

[...]
-cd




Schobi


-
Sp******@gmx.de 永远不会被阅读

我是Schobi at suespammers org


为什么我现在应该知道更好/什么时候我不够老?

Beth Orton


I''m trying to upgrade some old code that used old iostreams.

At one place in the code, I have a path/filename in a wchar_t string
(unicode utf-16).

I need to open an ifstream to that file. But the open() on ifstream only
takes char * strings (mbcs?).

In old iostreams, I could _wopen() the file, get the filedesc, and call
attach() on the ifstream.
But filedescs and attach() don''t exist in standard iostreams.

I can''t just convert the unicode string to a mbcs string, because the user''s
default codepage might not allow for all the characters that are in the
unicode string.

I can of course convert the unicode string to utf-8, but I can''t find any
way to get the iostream open() to believe that the string is utf-8.

trying to ibue() a stream with a custom locale that has a custom codecvt
seems to only affect data written to the stream, not how file names passed
to open are interpreted.

Does anyone know how I can accomplish opening an ifstream to a unicode named
file?

Why doesn''t ifstream have a wopen() ?

解决方案

Charles F McDevitt wrote:

I''m trying to upgrade some old code that used old iostreams.

At one place in the code, I have a path/filename in a wchar_t string
(unicode utf-16).

I need to open an ifstream to that file. But the open() on ifstream
only takes char * strings (mbcs?).

In old iostreams, I could _wopen() the file, get the filedesc, and
call attach() on the ifstream.
But filedescs and attach() don''t exist in standard iostreams.

I can''t just convert the unicode string to a mbcs string, because the
user''s default codepage might not allow for all the characters that
are in the unicode string.

I can of course convert the unicode string to utf-8, but I can''t find
any way to get the iostream open() to believe that the string is
utf-8.

trying to ibue() a stream with a custom locale that has a custom
codecvt seems to only affect data written to the stream, not how file
names passed to open are interpreted.

Does anyone know how I can accomplish opening an ifstream to a
unicode named file?

Why doesn''t ifstream have a wopen() ?



Because the C++ community can''t agree on how such a function should work.
For a system such as Windows that has a Unicode file system, the desired
behavior seems obvious: just pass the string to the filesystem. But since
C++ tries to address a much wider range of systems, many of which don''t
support unicode filesystems, we''re left with a C++ standard in which there
is no standard compliant, portable way to open a file given a unicode file
name.

The library implementation supplied with VC does provide a way to do it,
however. Open the file using the C runtime library _wfopen, then create the
ifstream by passing the FILE* that _wfopen() returns to the ifstream
constructor.

#include <fstream>

void foo(const wchar_t* pwsz)
{
std::ifstream stm(_wfopen(pwsz,L"rb"));

// Do stuff with the stream
}

-cd



"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...

Charles F McDevitt wrote:

I''m trying to upgrade some old code that used old iostreams.

At one place in the code, I have a path/filename in a wchar_t string
(unicode utf-16).

I need to open an ifstream to that file. But the open() on ifstream
only takes char * strings (mbcs?).

In old iostreams, I could _wopen() the file, get the filedesc, and
call attach() on the ifstream.
But filedescs and attach() don''t exist in standard iostreams.

I can''t just convert the unicode string to a mbcs string, because the
user''s default codepage might not allow for all the characters that
are in the unicode string.

I can of course convert the unicode string to utf-8, but I can''t find
any way to get the iostream open() to believe that the string is
utf-8.

trying to ibue() a stream with a custom locale that has a custom
codecvt seems to only affect data written to the stream, not how file
names passed to open are interpreted.

Does anyone know how I can accomplish opening an ifstream to a
unicode named file?

Why doesn''t ifstream have a wopen() ?
Because the C++ community can''t agree on how such a function should work.
For a system such as Windows that has a Unicode file system, the desired
behavior seems obvious: just pass the string to the filesystem. But


since C++ tries to address a much wider range of systems, many of which don''t
support unicode filesystems, we''re left with a C++ standard in which there
is no standard compliant, portable way to open a file given a unicode file
name.

The library implementation supplied with VC does provide a way to do it,
however. Open the file using the C runtime library _wfopen, then create the ifstream by passing the FILE* that _wfopen() returns to the ifstream
constructor.

#include <fstream>

void foo(const wchar_t* pwsz)
{
std::ifstream stm(_wfopen(pwsz,L"rb"));

// Do stuff with the stream
}

-cd



Thanks.. I guess that''s my only choice.


"Carl Daniel [VC++ MVP]" <cp******@nospam.mvps.org> wrote:

[...]

Why doesn''t ifstream have a wopen() ?
Because the C++ community can''t agree on how such a function should work.
For a system such as Windows that has a Unicode file system, the desired
behavior seems obvious: just pass the string to the filesystem. But since
C++ tries to address a much wider range of systems, many of which don''t
support unicode filesystems, we''re left with a C++ standard in which there
is no standard compliant, portable way to open a file given a unicode file
name.



I consider that a weak argument. After all,
C++ also tries to address systems where there''s
no file system at all.
(Carl, I know you''re not the one to argue with
about that. However, I just couldn''t let that
pass uncommented. <g>)
[...]
-cd



Schobi

--
Sp******@gmx.de is never read
I''m Schobi at suespammers org

"And why should I know better by now/When I''m old enough not to?"
Beth Orton


这篇关于iostream问题:如何打开unicode文件名。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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