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
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.