944,181 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5271
  • C++ RSS
Apr 26th, 2007
0

Scan File for Comments

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007
Apr 26th, 2007
0

Re: Scan File for Comments

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Apr 27th, 2007
0

Re: Scan File for Comments

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. }
Reputation Points: 10
Solved Threads: 0
Light Poster
RaCheer is offline Offline
31 posts
since Feb 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compiling gcc/c++
Next Thread in C++ Forum Timeline: Parents/Children - Processes/Threads





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC