954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Scan File for Comments

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();
}
RaCheer
Light Poster
31 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

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

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

<ul><li>#include</li>
</ul><ul><li><iostream></li>
<li>#include<fstream></li>
<li>usingnamespace std;</li>
<li>int main()</li>
<li>         {</li>
<li>         std::string line;</li>
<li>         ifstream InStream;</li>
<li>         cout << "Enter input file name: " ;</li>
<li>         getLine();</li>
<li>         // Open file for input</li>
<li>         InStream.open(InFile, ios::in);</li>
<li>        // ensure file is opened successfully</li>
<li>        if(!InStream)</li>
<li>        {</li>
<li>           cout << "Error open file " << InFile << endl;</li>
<li>           return 1;</li>
<li>        }</li>
<li>        while( getline(InStream,line) )</li>
<li>        {</li>
<li>              std::string::size_type pos = line.find("//");</li>
<li>              if(pos != std::string::npos)</li>
<li>        {</li>
<li>              std::string word = line.substr(pos);</li>
<li>              cout << word;</li>
<li>}</li>
<li>}</li>
</ul>
RaCheer
Light Poster
31 posts since Feb 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You