Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Deriving a new ifstream class (http://www.programmingforums.org/showthread.php?t=15272)

LENIN Feb 28th, 2008 1:31 AM

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.

OpenLoop Feb 28th, 2008 10:37 AM

Re: Deriving a new ifstream class
 
Why not create a separate read() function in your program that accepts the ifstream object pointer as input. The function can then read a line from the file, if it's a comment ignore it and get the next one, else return the read line.


All times are GMT -5. The time now is 7:45 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC