Solved

file input: get() after seekg()

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 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.

749 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