文件结束检查不起作用(C ++)

时间:2016-10-25 17:02:30

标签: c++ exception file-io

我们有一个带整数的文件。每行都有一个整数。我们必须浏览文件并找到最小和最大整数。一切都清楚该怎么做,但是当文件为空时我遇到了问题。

这是我的代码:

#include <iostream>
#include <fstream>
using namespace std;

void calculateMinMax(ifstream& inputStream, int& minNum, int& maxNum)
{
  string currLine;
  if(inputStream.ios::eof()) // code never enters this, even if file is empty !???
  {
    throw "EmptyFileException";
  }

  int currNum;

  inputStream >> minNum;
  maxNum = minNum;

  while(getline (inputStream, currLine))
  {
    inputStream >> currNum;

    if(currNum > maxNum)
    {
      maxNum = currNum;
    }

    if(currNum < minNum)
    {
      minNum = currNum;
    }
  }

  inputStream.close();
}

int main()
{
  int currNum, minNum, maxNum;
  ifstream inputStream("numberFile.txt");

  try
  {
    calculateMinMax(inputStream, minNum, maxNum);
    cout << "Min: " << minNum << endl;
    cout << "Max: " << maxNum << endl;
  }
  catch(...)
  {
    cout << "File is empty" << endl;
  }

  return 0;
}

我在检查文件是否为空的行上。我不知道为什么会这样,它只是继续执行而没有例外,即使它是空的。有什么建议吗?

2 个答案:

答案 0 :(得分:2)

有一个更简单的解决方案。您可以使用>>直接将数字读取到int。到达EOF时返回false。

void calculateMinMax(ifstream& inputStream, int& minNum, int& maxNum)
{
  int currNum;

  if(inputStream >> currNum)
  {
    maxNum = currNum;
    minNum = currNum;
  }
  else // there is not first number
  {
    throw "EmptyFileException";
  }

  while(inputStream >> currNum)
  {
    inputStream >> currNum;

    if(currNum > maxNum)
      maxNum = currNum;

    if(currNum < minNum)
      minNum = currNum;
  }

  inputStream.close();
}

答案 1 :(得分:2)

if(inputStream.ios::eof()) // code never enters this, even if file is empty !???
{
    throw "EmptyFileException";
}

这很简单的原因。在找到文件末尾之前,文件流无法知道您已到达文件末尾。找到文件末尾的唯一方法是从文件中读取,直到读取失败,因为已达到结束。

如果文件为空,则第一次读取将失败,并且将设置EOF位。您可以使用Checking for an empty file in C++

中涵盖的peek进行检查

偏离主题:这是笨重的:inputStream.ios::eof()inputStream.eof()已足够且可能更安全,因为std::ifstream覆盖eof(),可以使用比检查EOF位更重要的内容来覆盖# Enable upgrading of connection (and websocket proxying) depending on the # presence of the upgrade field in the client request header map $http_upgrade $connection_upgrade { default upgrade; '' close; } # Create an upstream alias to where we've set daphne to bind to upstream django_app_server { server 127.0.0.1:8000; } server { listen 80; server_name YOURDOMAIN.COM; client_max_body_size 4G; access_log /webapps/General/logs/nginx-access.log; error_log /webapps/General/logs/nginx-error.log; location /static/ { alias /webapps/General/DjangoProject/static/; } location /media/ { alias /webapps/General/DjangoProject/media/; } location / { if (!-f $request_filename) { proxy_pass http://django_app_server; break; } # Require http version 1.1 to allow for upgrade requests proxy_http_version 1.1; # We want proxy_buffering off for proxying to websockets. proxy_buffering off; # http://en.wikipedia.org/wiki/X-Forwarded-For proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # enable this if you use HTTPS: # proxy_set_header X-Forwarded-Proto https; # pass the Host: header from the client for the sake of redirects proxy_set_header Host $http_host; # We've set the Host header, so we don't need Nginx to muddle # about with redirects proxy_redirect off; # Depending on the request value, set the Upgrade and # connection headers proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } # Error pages error_page 500 502 503 504 /500.html; location = /500.html { root /webapps/General/DjangoProject/templates/; } }