C++ 文件读取内部/外部,而两个读取语句(逻辑?)
好吧,我的问题是,我只是对发生的事情感到困惑。如果你看一下整个while循环,我不明白第一个read语句的目的是什么,而在内部while循环中也有一个read语句。整个内部while循环不负责所有的读取吗?在阅读内部while之前阅读帐户、名称和余额的目的是什么 糟糕的是,该程序只处理信贷经理的请求,并为余额与请求相符的人显示相应的记录C++ 文件读取内部/外部,而两个读取语句(逻辑?),c++,file,loops,file-io,C++,File,Loops,File Io,好吧,我的问题是,我只是对发生的事情感到困惑。如果你看一下整个while循环,我不明白第一个read语句的目的是什么,而在内部while循环中也有一个read语句。整个内部while循环不负责所有的读取吗?在阅读内部while之前阅读帐户、名称和余额的目的是什么 糟糕的是,该程序只处理信贷经理的请求,并为余额与请求相符的人显示相应的记录 #include <iostream> #include <fstream> #include &l
#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;
循环首先从文件中读取值,然后检查最后一次读取操作是否导致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。while=inClientFile.eof您确定这是真实代码吗???@BoBTFish错误,谢谢,正在修复。就写在这里,所以xD。哦,这更有意义。哈哈,看起来循环在做其他事情,比如一些初步的事情。谢谢你!如果这两种说法相反,你就不能用同样的论点吗?我不能不考虑顺序地读取无效数据吗?Thanks@Shinji-请检查此链接。好的,请原谅我。我有一个愚蠢的问题><,所以我不明白第一个read语句读了多少。例如,第一条read语句,是否像内部read语句一样读取所有帐户?是的,提取运算符一直工作,直到有一个有效的输入要读取。当发生错误或eof时,它停止。