我寫下面的代碼....FILESTREAM處理
#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'的輸出重複......爲什麼這樣呢?
這是一些失敗。 http://www.cplusplus.com/reference/iostream/istream/tellg/ – balki
代碼+輸出+爲什麼? !=好SO問題 – AJG85
@balki太短解釋...我知道失敗返回-1,但我已經把條件,當EOF面臨循環將終止...但你可以看到,即使55後是輸出(即在包含aditya的語句被輸入到流中之後)循環不會終止,但會給出一個更多的輸出-1 ...並且當我輸入3時重複相同的操作...並且最重要的是我問了兩個問題。 ..u應該已經回答了令人滿意的問題,然後再投降我 – avinash