/* Copyright: 2000, All rights reserved. Program: Comments Version: 1.0 Created/Revised: 10/10/00 File: comments.cpp Programmer: C. S. Tritt, Ph.D. Compiler: Microsoft Visual C++ 6.0 Target: Win32 Description: This program demonstrates the reading of data files containing comments. Revisions: There have been no major revisions of this program. Constants and Variables: See code for constant and variable descriptions. */ // Included files. #include // Header for console I/O #include // Header for file I/O. #include // Header for STL strings. // Macros for ANSI C++ logical operations in MSVC++ 6.0. #define and && #define or || #define not ! using namespace std; // Define skip_comments function. int skip_comments(ifstream& file, char mark) { /* This function skips the all the lines at the start of the specified file that begin with the specified character. The file must already be open when this funciton is called. Error checking not yet included. */ const int MAX_CHARS = 100; // This is the maximum characters per line. while (file.peek() == mark) {file.ignore(MAX_CHARS, '\n');} return 0; } int main() { // Define constants. // Open input file. ifstream fin; // Declare an input file object. fin.open("sometext.txt", ios::in); // Open the input file in project folder. if (!fin) // See if the file was opened sucessfully. { cout << "Can't open input file. Aborting!\n"; return 1; } cout << "About to skip comments.\n"; skip_comments(fin,'!'); string sometext; getline(fin, sometext); while (!fin.fail()) { cout << sometext << '\n'; getline(fin, sometext); } if (fin.fail() && !fin.eof()) { cout << "Error while reading file. Aborting!\n"; fin.close(); return 2; } // Everything worked. return 0; }