Solved

file input: get() after seekg()

Posted on 1998-07-12
2
226 Views
Last Modified: 2010-04-10
After a file is traversed with get() I use seekg() to return to the beginning of the file.  This works okay, but when I use get() after I reach the beginning of the file, get() just doesn't get it (so to speak).  I don't understand this.  Here is some sample code compiled with DJGPP (hope it shows correctly):

#include <iostream.h>
#include <fstream.h>

main()
{
  char nextChar;
  ifstream readIn("gallery.txt");

  if(!readIn)
  {
     cout << "Can't find the file \"gallery.txt\"" << endl;
     return 0;
  }

  while(readIn.get(nextChar))
  {
    cout << nextChar;
  }

  cout << endl << "Place before seekg(): " << readIn.tellg() << endl;
  readIn.seekg(0, ios::beg);
  cout << endl << "Place after seekg(): " << readIn.tellg() << endl;

  cout << "=====================================================" << endl
       << "=====================================================" << endl;

  while(readIn.get(nextChar))
  {
    cout << nextChar;
  }

  return 0;
}

How do I get input from the second readIn.get()?  Why is it not inputting?  Is there something I'm doing wrong?
0
Comment
Question by:faehntim
2 Comments
 

Accepted Solution

by:
mehlert earned 100 total points
ID: 1167598
Set up your while loops like this:

  while(readIn.peek() != EOF)
  {
     readIn.get(nextChar);      
     cout << nextChar;
  }

You need to peek for the end of the file instead of getting it.
-Matt
0
 
LVL 2

Expert Comment

by:mlev
ID: 1167599
This happens because the end-of-file flag is still set for readIn.
Do readIn.clear() to clear it (before or after seekg()).
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now