2011-04-20 50 views
0

可能重复:
How to open an std::fstream (ofstream or ifstream) with a unicode filename ?打开与fstream的,但文件名字符的文本文件不是ASCII

我想用C++的fstream打开一个文本文件,但性格用于文件名不属于ASCII字符集。 例如:

fstream fileHandle; 
fileHandle.open("δ»Wüste.txt"); 

有没有办法存在,通过它我可以打开文件,这样的名字。

感谢 维韦克

+0

@Ephphatha看到的,这个问题的描述正确。它不是重复的。 – 2011-04-20 09:00:44

+0

它看起来和我完全一样,但无论如何我都会回答。 – Ephphatha 2011-04-20 09:05:03

+1

文件名固有地系统特定,并且取决于底层文件系统支持的内容。该语言无法定义。 – 2011-04-20 09:47:19

回答

2

从问题How to open an std::fstream with a unicode filename @jalf指出,C++标准库不是Unicode知道,但有一个窗口延伸接受wchar_t数组。

您将能够通过在wstream_t数组作为参数的fstream对象上创建或调用open来打开Windows平台上的文件。

fstream fileHandle(L"δ»Wüste.txt"); 
fileHandle.open(L"δ»Wüste.txt"); 

上述两种将调用的wchar_t *的相应的功能的版本,如在串将L前缀指示其是被当作一个Unicode字符串。

编辑:这是一个应该编译和运行的完整示例。我在我的计算机上创建了一个名为δ»Wüste.txt的文件,其内容为This is a test.然后我编译并在同一目录中运行以下代码。

#include <fstream> 
#include <iostream> 
#include <string> 

int main(int, char**) 
{ 
    std::fstream fileHandle(L"δ»Wüste.txt", std::ios::in|std::ios::out); 

    std::string text; 
    std::getline(fileHandle, text); 
    std::cout << text << std::endl; 

    system("pause"); 

    return 0; 
} 

的输出是:

This is a test. 
Press any key to continue... 
+0

我需要这个fstream。我已经经历了与此相关的最后一篇文章。 – dearvivekkumar 2011-04-20 09:23:41

+1

我不知道你在问什么,对不起,我提供的例子适用于Windows系统,并使用fstream。 – Ephphatha 2011-04-20 09:40:08

1

在Windows上,你可以使用长字符串:

fileHandle.open(L"δ»Wüste.txt");