【发布时间】:2011-06-28 14:35:15
【问题描述】:
我写了以下代码....
#include< iostream>
#include< fstream>
using namespace std;
int main()
{
ifstream in("text1.dat",ios::in);
enum choice{zero=1, credit, debit, exit};
choice your;
int balance;
char name[50];
int option;
while(cin>>option)
{
if(option==exit)
break;
switch(option)
{case zero:
while(!in.eof())
{in>>balance>>name;
if(balance==0)
cout<<balance<<" "<<name<<endl;
cout<<in.tellg()<<endl;
}
in.clear();
in.seekg(0);
break;}
// likewise there are cases for debit and credit
system("pause");
return 0;
}
在 text1.dat 中的条目是:
10 avinash
-57 derek
0 fatima
-98 gorn
20 aditya
输出是:
1 //i input this
16
27
0 fatima
36
45
55
-1 //(a)
3 //i input this
10 avinash
16
27
36
45
20 aditya
55
20 aditya //(b)
-1
我的问题是:
- 标记为“a”的输出是-1...-1 作为tellg() 的输出是什么意思?
- 标记为“b”的输出重复...为什么会这样?
【问题讨论】:
-
代码 + 输出 + 为什么? != 好问题
-
@balki 解释太短了...我知道失败返回-1,但我已经提出条件,当遇到 EOF 时,循环将终止...但你甚至可以看到之后55 是输出(即在将包含 aditya 的语句输入到流中之后)循环不会终止,但会再提供一个输出 -1 ......当我输入 3 时重复同样的事情......最重要的是我问了两个问题...在否决我之前,你应该已经满意地回答了这两个问题
标签: c++ file-processing