Scan File for Comments

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Scan File for Comments

 
0
  #1
Apr 26th, 2007
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!

  1.  
  2. #include <iostream>
  3. #include <fstream>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. char InFile[80]; // input file name
  9. char ch;
  10.  
  11. ifstream InStream;
  12. cout << "This program reads the content of a file and ";
  13. cout << "prints only the comments out on the screen."
  14. << endl;
  15. cout << "Enter input file name: " ;
  16. cin >> InFile;
  17. // Open file for input
  18.  
  19. InStream.open(InFile, ios::in);
  20. // ensure file is opened successfully
  21. if(!InStream)
  22. {
  23. cout << "Error open file " << InFile << endl;
  24. return 1;
  25. }
  26.  
  27. cout << "Here are the comments of " << InFile << ":\n";
  28. // Read in each character until eof character is read.
  29. // Output it to screen.
  30. while (!InStream.eof()) {
  31. //Read each character.
  32. InStream.get(ch);
  33. if (!InStream.eof())
  34. {
  35. cout << ch; //Write to screen
  36. }
  37. }
  38. InStream.close();
  39. }
Last edited by Ancient Dragon; Apr 26th, 2007 at 10:15 pm. Reason: corrected code tags to show line numbers
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,485
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Scan File for Comments

 
0
  #2
Apr 26th, 2007
>>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
  1. std::string line;
  2. while( getline(InStream,line) )
  3. {
  4. std::string::size_type pos = line.find("//");
  5. if(pos != std::string::npos)
  6. {
  7. std::string word = line.substr(pos);
  8. cout << word;
  9. }
  10. }

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.
Last edited by Ancient Dragon; Apr 26th, 2007 at 10:22 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 31
Reputation: RaCheer is an unknown quantity at this point 
Solved Threads: 0
RaCheer RaCheer is offline Offline
Light Poster

Re: Scan File for Comments

 
0
  #3
Apr 27th, 2007
I've tried playing with it and it still hates me...lol!

 
  1. #include
  1. <iostream>
  2. #include<fstream>
  3. usingnamespace std;
  4. int main()
  5. {
  6. std::string line;
  7. ifstream InStream;
  8. cout << "Enter input file name: " ;
  9. getLine();
  10. // Open file for input
  11. InStream.open(InFile, ios::in);
  12. // ensure file is opened successfully
  13. if(!InStream)
  14. {
  15. cout << "Error open file " << InFile << endl;
  16. return 1;
  17. }
  18. while( getline(InStream,line) )
  19. {
  20. std::string::size_type pos = line.find("//");
  21. if(pos != std::string::npos)
  22. {
  23. std::string word = line.substr(pos);
  24. cout << word;
  25. }
  26. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC