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();
}

>>The code I have written so far works, but it prints the entire code

If it prints the entire code then it doesn't work, does it.;)

line 8: this is a c++ program, so replace the char array with std::string, which will allow you to enter a file name of any length up to the limit set by the operating system.

Line 16: if the file name contains spaces then that line will not work. Safer if you use getline().

lines 30-37: not a good way to detect eof. Don't read the file one character at a time -- that's too much work. Here's a simpler way to do this

std::string line;
while( getline(InStream,line) )
{
      std::string::size_type pos = line.find("//");
      if(pos != std::string::npos)
      {
           std::string word = line.substr(pos);
           cout << word;
     }
}

This doesn't solve the /**/ type comments but should work the // style commens. This was not compiled or tested, so use the above at your own risk.

I've tried playing with it and it still hates me...lol!

[LIST=1]
[*]#include <iostream>
[*]#include <fstream>
[*]using namespace std;
[*]int main()
[*]         {
[*]         std::string line;
[*]
[*]         ifstream InStream;
[*]
[*]         cout << "Enter input file name: " ;
[*]         getLine();
[*]         // Open file for input
[*]
[*]         InStream.open(InFile, ios::in);
[*]        // ensure file is opened successfully
[*]        if(!InStream)
[*]
[*]        {
[*]           cout << "Error open file " << InFile << endl;
[*]           return 1;
[*]        }
[*]
[*]
[*]        while( getline(InStream,line) )
[*]        {
[*]              std::string::size_type pos = line.find("//");
[*]              if(pos != std::string::npos)
[*]        {
[*]              std::string word = line.substr(pos);
[*]              cout << word;
[*]}
[*]
[*]}[/LIST]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.