Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Feb 28th, 2008, 10:37 AM   #2
OpenLoop
Expert Programmer
 
OpenLoop's Avatar
 
Join Date: May 2005
Location: East Lansing, MI
Posts: 663
Rep Power: 4 OpenLoop is on a distinguished road
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.
OpenLoop is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
template class brad sue C++ 1 Mar 25th, 2007 5:46 PM
URL class Eric the Red Java 5 Jun 24th, 2006 9:01 PM
Problem with class constructor paeck C++ 8 Feb 7th, 2006 12:53 PM
using classes in another class ling_wong C++ 6 Jan 23rd, 2006 9:55 PM
MFC/OpenGL: problem with 'Document' class brenda C++ 11 May 23rd, 2005 8:10 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:10 AM.

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