?
Solved

file input: get() after seekg()

Posted on 1998-07-12
2
Medium Priority
?
232 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
mehlert earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

800 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