【问题标题】:std::stringstream not reading whitespace [duplicate]std::stringstream 不读取空格 [重复]
【发布时间】:2015-11-27 22:28:38
【问题描述】:
    template< typename T >
    T Read( const char* Section, const char* Key )
    {
        SecureZeroMemory( m_Result, sizeof( m_Result ) );
        GetPrivateProfileString( Section, Key, 0, m_Result, sizeof( m_Result ), m_File.FullPath );
        std::istringstream Cast( m_Result );
        T Result{ };
        Cast >> std::noskipws >> Result;
        return Result;
    }

m_Result 是我班级的成员变量。 (字符[256])。

目标:尝试返回我在模板 arg 上插入的所有类型。

问题:当我发送带有“Example Text Return”的 std::string 时,它会返回“Example”而不是“Example Text Return”。

错误在哪里?我尝试了很多skipws或noskipws或ws...

对不起英语,我是巴西人。

【问题讨论】:

  • @Cainan :这里的第一个问题很好..

标签: c++ visual-studio-2015 whitespace


【解决方案1】:

std::noskipws 只读取任何初始空白。 std::string&gt;&gt; 运算符重载总是在第一个遇到的空白字符处停止读取字符串。 std::noskipws 使其读取初始空白,但转换仍会在第一个非空白字符之后的第一个空白字符处停止。

您需要做的是专门针对std::string 的这个模板函数,并且只返回m_Result 而不进行任何转换。

【讨论】:

  • 好的,但我想像这样使用该函数: int Key = Read( "Section", "Key" );这就是我使用模板函数的原因。但如果没有其他解决方案,我会为 const char* 重载。
  • 我的回答中没有任何内容与您的意图相反。您的模板函数保持不变,所需要的只是 std::string 的显式特化。顺便说一句,不是 const char *。
  • 谢谢,我找到了解决办法。我不能投票给你的答案:(抱歉呵呵。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-03
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 2018-10-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多