To extract and discard a number of characters.
basic_istream<charT, traits>& ignore (steamsize n = 1, int_type delim = traits::eof());
The function ignore() will extract and discard characters until:
The next character c is equal to the delimiter delim, in which case it is extracted except when c is equal to traits::eof();
The this pointer is returned.
// The file ewl-test contains: // char ch; // to save char // /*This C comment will remain */ // while((ch = in.get())!= EOF) cout.put(ch); // // read until failure // /* the C++ comments won't */ #include <iostream> #include <fstream> #include <cstdlib> char inFile[] = "ewl-test"; char bslash = '/'; int main() { using namespace std; ifstream in(inFile); if(!in.is_open()) {cout << "file not opened"; exit(1);} char ch; while((ch = in.get()) != EOF) { if(ch == bslash && in.peek() == bslash) { in.ignore(100, '\n'); cout << '\n'; } else cout << ch; } return 0; }
Result:
char ch;
/*This C comment will remain */
while((ch = in.get())!= EOF) cout.put(ch);
/* the C++ comments won't */