Solved

file input: get() after seekg()

Posted on 1998-07-12
2
228 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
Move constructor only called if marked noexcept? 6 104
I could not build boost code, 10 87
Header of docx file 17 102
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

786 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