【问题标题】:How do I widen a char in c++如何在 C++ 中加宽字符
【发布时间】:2015-03-13 17:01:24
【问题描述】:

我正在使用 SFML 用 c++ 编写游戏,我发现了一种支持法语字符的字体。但是,在程序中,我从文件中读取了所有文本以便能够支持不同的语言,但我不知道如何将没有错误的文本提取为宽字符串。

这是我的代码:

using namespace std;
using namespace sf;

void SettingsW :: initialize()
{

// using normal characters it reads the files correctly

    wifstream settTextFile;
    settTextFile.open(pageTextSource);
    wstring temp;

    getline(settTextFile, temp);
    pageTitle.setFont(pageFont);
    pageTitle.setString(temp);

    getline(settTextFile, temp, L' ');
    languageTitle.setFont(pageFont);
    languageTitle.setString(temp);

//here is the problem
    char g=' ';
    ios::widen(g);
    getline(settTextFile, temp, ' '));  
// I want to use get line with this delimiter and when I use the L the error goes away 
//but it doesn't display properly: é is displayed as ã
}

【问题讨论】:

    标签: c++ char sfml wchar wifstream


    【解决方案1】:

    不太清楚你的问题是什么。您提供的代码 不应该编译; ios::widen 是成员函数,可以 只能在 ios 上调用(这是一个 typedef std::basic_ios&lt;char&gt;,其中没有实例 代码)。此外,ios::widen 返回加宽字符,除了 ios::widen(相对于 std::basic_ios<wchar_t>::widen) doesn't widen, since it returns achar. If you want to use the character ingthe delimiter in the last call tostd::getline`,那么你可以使用:

    std::getline( settTextFile, tmp, settTextFile.widen( g ) );
    

    (当然,你应该验证std::getline成功 在使用它读取的值之前。)

    关于“显示不正确”: 你必须提供更多关于你的情况的信息 为我们确定显示它,但在我看来很可能 你只是没有给你的输出流灌输相同的东西 编码为窗口的代码页(假设是 Windows),或 使用窗口中使用的字体的编码(假设 Unix)。但你必须向我们展示你的真实身份 显示,你如何显示它,并给我们一些 如果您想获得完整的答案,请提供有关环境的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-30
      • 2012-10-09
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多