摆脱超载的提取运算符? (C++)

发布于 2024-08-21 17:09:31 字数 753 浏览 7 评论 0原文

我正在尝试使用重载的“>>”扫描文件中的输入。

问题是,我不知道如何处理文件结尾。 在这种情况下,我的文件由一个数字组成,后跟几个字符

例如:

9rl

8d

6ff

istream &operator>>(istream &is, Move &move)
{
  char c;
  int i = 0;

  c = is.get();

  if (!isalnum(c))
      return;

  move.setNum(c); // I convert the char to an int, but I'l edit it out

  while ( (c = is.get()) != '\n')
  {
    move.setDirection(i, c); //sets character c into in array at index i
    i++;

  } // while chars are not newline

  return is;
} // operator >>

当我将其作为常规函数时,对字母数字字符的测试有效,但在这里不起作用,因为它需要输入要返回的流。我也尝试过返回 NULL。建议?

编辑:这是在 while 循环中调用的,所以我试图找出某种方法来让这个触发一些标志,以便我可以跳出循环。在我之前的函数中,我返回一个布尔值,如果成功则返回 true,如果字符不是字母数字则返回 false

I'm trying to use an overloaded ">>" to scan input from a file.

The problem is, I have no idea how to deal with end of file.
In this case, my file is composed of a number, followed by several chars

Ex:

9rl

8d

6ff

istream &operator>>(istream &is, Move &move)
{
  char c;
  int i = 0;

  c = is.get();

  if (!isalnum(c))
      return;

  move.setNum(c); // I convert the char to an int, but I'l edit it out

  while ( (c = is.get()) != '\n')
  {
    move.setDirection(i, c); //sets character c into in array at index i
    i++;

  } // while chars are not newline

  return is;
} // operator >>

The test for the character being alpha numeric worked when I had this as a regular function, but doesn't work here as it expects an input stream to be returned. I've tried returning NULL as well. Suggestions?

EDIT: this is being called in a while loop, So i'm trying to figure out some way to have this trigger some flag so that I can break out of the loop. In my previous function, I had this return a boolean, returning true if successful or false if the character was not alphanumeric

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

东京女 2024-08-28 17:09:31

返回。调用者应该检查流是否有错误。

请务必根据需要设置错误位:

std::istream &operator>>(std::istream &is, Move &move)
{
  char c;
  int i = 0;

  c = is.get();
  if (is.eof())
    return is;
  else if (c < '0' || c > '9') {
    is.setstate(std::ios::badbit);
    return is;
  }
  else
    move.setNum(c-'0');

  while ( (c = is.get()) != '\n' && is)
    move.setDirection(i++, c);

  if (c != '\n')
    is.setstate(std::ios::badbit);
  return is;
}

按以下方式使用它:

int main(int argc, char **argv)
{
  std::stringstream s;

  s << "9rl\n"
    << "8d\n"
    << "6ff\n";
  s.seekg(0);

  Move m;
  while (s >> m)
    std::cout << m;

  if (s.bad())
    std::cerr << argv[0] << ": extraction failed\n";

  return 0;
}

请注意,代码仅在成功提取后才使用实例 m

Return is. Callers should check the stream for errors.

Be sure to set error bits as appropriate:

std::istream &operator>>(std::istream &is, Move &move)
{
  char c;
  int i = 0;

  c = is.get();
  if (is.eof())
    return is;
  else if (c < '0' || c > '9') {
    is.setstate(std::ios::badbit);
    return is;
  }
  else
    move.setNum(c-'0');

  while ( (c = is.get()) != '\n' && is)
    move.setDirection(i++, c);

  if (c != '\n')
    is.setstate(std::ios::badbit);
  return is;
}

Use it as in the following:

int main(int argc, char **argv)
{
  std::stringstream s;

  s << "9rl\n"
    << "8d\n"
    << "6ff\n";
  s.seekg(0);

  Move m;
  while (s >> m)
    std::cout << m;

  if (s.bad())
    std::cerr << argv[0] << ": extraction failed\n";

  return 0;
}

Notice that the code uses the instance m only after successful extraction.

[浮城] 2024-08-28 17:09:31

您可以将流的标志设置为诸如 ios::bad 或 ios::fail 使用 ios::setstate。这将允许调用者测试流,或者在为流启用异常的情况下,将引发异常。

您也无法检查流的状态。 C++ FAQ lite 有一个很棒的部分解释了这一点。为了澄清这一点,我添加了下面的代码片段。

c = is.get();
// the stream has not been tested to see if it read into c correctly
if (!isalnum(c))
    return;

You can set the flags of the stream to a state such as ios::bad or ios::fail using ios::setstate. This will allow the caller to test the stream or in the case that exceptions are enabled for the stream, an exception will be raised.

You also fail to check the state of your stream. The C++ FAQ lite has a great section explaining this. To clarify this I have added the code snippet below.

c = is.get();
// the stream has not been tested to see if it read into c correctly
if (!isalnum(c))
    return;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文