c++中的文件流处理(tellg() 函数)

发布于 2024-11-17 16:14:21 字数 1104 浏览 1 评论 0原文

我写了以下代码...

#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  

我的问题是:

  1. 标记为“a”的输出是-1...-1作为tellg()的输出意味着什么?
  2. 标记为“b”的输出被重复...为什么会这样?

i wrote the following code....

#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;   
}    

In text1.dat the entry was:

10 avinash  
-57 derek  
0 fatima  
-98 gorn  
20 aditya

and the output was:

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  

my questions are:

  1. the output marked 'a' is -1...what does -1 mean as an output of tellg()?
  2. the output marked 'b' is repeated...why so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

時窥 2024-11-24 16:14:21

您观察到的行为与许多其他新手 C++ 程序员相同。例如,阅读这个问题

发生的情况是,在您尝试从 inin.eof() 设置为 true > 并且操作失败,因为没有更多数据。当读取操作由于文件结束而失败时,它会设置 botheofbitfailbit。当流处于失败状态时,tellg 函数会返回 -1

要解决此问题,请在执行读取操作之后、执行其他操作之前测试 eof。更好的是,检查操作是否只是“失败”,因为您不想区分文件结尾和不正确的输入(例如,如果输入的是字符串而不是余额数字,则您的代码会输入一个无限循环):

for(;;)
{
  in>>balance>>name;
  if(!in)
    break;
  if(balance==0)
    cout<<balance<<" "<<name<<endl;
  cout<<in.tellg()<<endl;
}

!in 条件检查是否设置了 failbitbadbit。您可以通过重写来简化它:

while(in>>balance>>name)
{
  if(balance==0)
    cout<<balance<<" "<<name<<endl;
  cout<<in.tellg()<<endl;
}

You are observing the same behavior as many other novice C++ programmers. Read for example this question.

What happens is that in.eof() is set to true after you've tried to read something from in and the operation failed because there was no more data. When a read operation fails due to end-of-file, it sets both, eofbit and failbit. When a stream is in fail state, the tellg function is documented to return -1.

To solve the problem, test for eof after you perform a read operation and before you do anything else. Even better, check that the operation just 'failed', since you don't want to distinguish between an end-of-file and an incorrect input (e.g. if a string is fed instead of an number for the balance, your code enters an infinite loop):

for(;;)
{
  in>>balance>>name;
  if(!in)
    break;
  if(balance==0)
    cout<<balance<<" "<<name<<endl;
  cout<<in.tellg()<<endl;
}

The !in condition checks that either failbit or badbit are set. You can simplify this by rewriting as:

while(in>>balance>>name)
{
  if(balance==0)
    cout<<balance<<" "<<name<<endl;
  cout<<in.tellg()<<endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文