【问题标题】:sscanf is well but std::scanf don't work [closed]sscanf 很好,但 std::scanf 不起作用[关闭]
【发布时间】:2014-05-09 04:51:02
【问题描述】:

我正在尝试使用 Visual Studio 2010 Pro 从文本文件中读取数据。 sscanf 运行良好,但 std::scanf 不行。

我不知道为什么会这样;请帮帮我。

float b, g, r;
int px = 0;
int py = 0;
std::string strLine;  // some string is.

//below code works very well.
int nRead = sscanf(strLine.c_str(), "%f %f %f %d %d", &b, &g, &r, &px, &py );

//but this does not.
int nRead = std::scanf(strLine.c_str(), "%f %f %f %d %d", &b, &g, &r, &px, &py );

【问题讨论】:

  • std::sscanf 怎么样?
  • 哦〜我很愚蠢。谢谢大家。

标签: c++ std scanf


【解决方案1】:

来自手册页:

   int scanf(const char *format, ...);
   int fscanf(FILE *stream, const char *format, ...);
   int sscanf(const char *str, const char *format, ...);
  • scanf:从标准输入读取
  • fscanf:从文件中读取
  • sscanf:从字符串中读取。

您调用 scanf 的方式,要读取的预期字符串被解释为格式规范,而格式规范被用作变量参数之一。

【讨论】:

    【解决方案2】:

    两个选择:

    int nRead = std::sscanf(strLine.c_str(), "%f %f %f %d %d", &b, &g, &r, &px, &py );
    

    int nRead = std::scanf("%f %f %f %d %d", &b, &g, &r, &px, &py );
    

    选择一个。

    【讨论】:

      【解决方案3】:

      std::scanf 用于从标准输入读取内容。

      std::sscanf 用于读取字符串。

      Click the link

      【讨论】:

        【解决方案4】:

        您没有正确使用它们。在 cppreference.com 上查看 the documentation 以获取 std::scanfstd::fscanfstd::sscanf

        您可能想使用:

        std::sscanf(...)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-15
          • 2016-12-29
          • 2012-04-01
          相关资源
          最近更新 更多