文件读取内/外时,两个读语句(逻辑?)

时间:2013-12-19 10:51:13

标签: c++ file loops file-io

好的我的问题是我对发生的事情感到困惑。如果你看一下整个while循环,我不明白第一个read语句的目的是什么,在内部while循环中也有一个read语句。整个内部while循环不能处理所有的读数吗?在内心之前阅读帐号,名称和平衡的目的是什么?

糟糕,该程序只处理一个信用经理的请求,并显示其余额与请求的余额相匹配的人的记录。

      #include <iostream>
      #include <fstream>
      #include <iomanip>
      #include <string>
      #include <cstdlib>
      using namespace std;

      enum RequestType { ZERO_BALANCE = 1, CREDIT_BALANCE, DEBIT_BALANCE, END };
      int getRequest();
      bool shouldDisplay( int, double );
      void outputLine( int, const string &, double );

      int main()
      {
          // ifstream constructor opens the file
          ifstream inClientFile("clients.txt", ios::in );

          // exit program if ifstream could not open file
          if ( !inClientFile )
          {
            cerr << "File could not be opened" << endl;
            exit( EXIT_FAILURE);
          } // end if

          int account; // the account number
          string name; // the account owner's name
          double balance; // the account balance

          // get user's request(e.g., zero, credit or debit balance)
          int request = getRequest();

          // process user's request
          while ( request != END )
          {
            switch ( request )
            {
              case ZERO_BALANCE:
                 cout << "\nAccounts with zerobalances: \n";
                 break;
              case CREDIT_BALANCE:
                 cout << "\nAccounts with credit balances:\n"
                 break;
              case DEBIT_BALANCE:
                 cout << "\nAccounts with debit balances:\n";
                 break;
            } // end switch

            // read account, name a balance from file(also this is what
            // confused about, like this is the statement that seems moot to me)
            inClientFile >> account >> name >> balance;

            // display file contents
            while ( !inClientFile.eof() )
            {
               // display record
               if ( shouldDisplay( request, balance ) )
                  outputLine( account, name, balance );

               // read account, name and balance from file(again
               // i thought this was the only required reading statement
               // because it goes until EOF 
               inClientFile >> account >> name >> balance;
               } // end inner while

               inClientFile.clear(); // reset eof for next input
               inClientFile.seekg( 0 ); // reposition to beginning of file
               request = getRequest(); // get additional request from user
          } // end outer while

          cout << "End of run." << endl;
} // end main

// obtain request from user
int getRequest()
{
  int request; // request from user

  // display request options
  cout << "\nEnter request" << endl
       << " 1 - List accounts with zero balances" << endl
       << " 2 - List of accounts with credit balances: << endl
       << " 3 - List of accounts with debit balances" << endl
       << " 4 - End of run" << fixed << showpoint;


 do // input user request
 {
   cout << "\n?;
   cin >> request;
 } while ( request < ZERO_BALANCE && request > END );

 return request;
} // end function getRequest


// determine whether to display given record
bool shouldDisplay( int type, double balance )
{
  // determine whether to display zero balances
  if ( type == ZERO_BALANCE && balance == 0 )
     return true;

  // determine whether to display credit balances
  if ( type == CREDIT_BALANCE && balance < 0 )
     return true;

  // determine whether to display debit balances
  if ( type == DEBIT_BALANCE && balance > 0 )
     return true;

  return false;
  // SIDE QUESTION PLZ*-why do you think the book didn't use else if
  // like wouldn't it make sense, or...? just curious, thank you

} // end function shouldDisplay

// display single record from file
void outputLine( int account, const string &name, double balance )
{
   cout << left <, setw( 10 ) << account << setw( 13 ) << name 
      << setw( 7 ) << setprecision( 2 ) << right << balance << endl;

1 个答案:

答案 0 :(得分:0)

循环首先从文件中读取值,然后检查上次读取操作是否导致eof。这就是读取代码在while循环之前和内部的原因。

我认为这个循环可以替换为:

// display file contents
while ( inClientFile >> account >> name >> balance )
{
   // display record
   if ( shouldDisplay( request, balance ) )
      outputLine( account, name, balance );

 } // end inner while

编辑:

另请注意,这是不正确的用法:

while ( !inClientFile.eof() )
{
   inClientFile >> account >> name >> balance;
   // display record
   if ( shouldDisplay( request, balance ) )
      outputLine( account, name, balance );
} // end inner while

这是因为虽然读取文件的输入结尾可能已达到,但我们将处理无效数据。因此,我们需要在每次读取操作后检查eof

因此,通常建议避免使用eof