I am trying to write a program that will scan another file for comments only. It will scan for comments of both types, // and /* */, and then print these comments. The code I have written so far works, but it prints the entire code. I would really appreciate a little guidance!
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char InFile[80]; // input file name
char ch;
ifstream InStream;
cout << "This program reads the content of a file and ";
cout << "prints only the comments out on the screen."
<< endl;
cout << "Enter input file name: " ;
cin >> InFile;
// Open file for input
InStream.open(InFile, ios::in);
// ensure file is opened successfully
if(!InStream)
{
cout << "Error open file " << InFile << endl;
return 1;
}
cout << "Here are the comments of " << InFile << ":\n";
// Read in each character until eof character is read.
// Output it to screen.
while (!InStream.eof()) {
//Read each character.
InStream.get(ch);
if (!InStream.eof())
{
cout << ch; //Write to screen
}
}
InStream.close();
}