View Single Post
Old Feb 28th, 2008, 1:31 AM   #1
LENIN
Newbie
 
LENIN's Avatar
 
Join Date: Dec 2005
Location: In the minds of all true communists.
Posts: 15
Rep Power: 0 LENIN is on a distinguished road
Deriving a new ifstream class

I would like to derive a new ifstream class, that would ignore comments in a file (lines that start with a '#' character). For instance:

Input file (f.txt)
#This is a comment
This is not

An example of use
int main(){
  
  string a;
  commentStream cs("f.txt");

  if(!cs) exit(1);
  else{
     while(cs){
        cs >> a;
        cout << a << endl;
     }
  }
  return (0);
}

Expected output
This
is
not

I know I'm suppose to derive a new streambuf and than derive the new stream using this streambuf. But I can't figure out how the new streambuf should look like (which methods should be overridden and what should they do). I think I should override the underflow method but I'm not exactly sure how.

Any help would be welcome.
__________________
Science progresses one death at a time - Niels Bohr
LENIN is offline   Reply With Quote