| | |
Scan File for Comments
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
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!
c Syntax (Toggle Plain Text)
#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(); }
Last edited by Ancient Dragon; Apr 26th, 2007 at 10:15 pm. Reason: corrected code tags to show line numbers
>>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
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.
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
c Syntax (Toggle Plain Text)
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.
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.
•
•
Join Date: Feb 2007
Posts: 31
Reputation:
Solved Threads: 0
I've tried playing with it and it still hates me...lol!
- #include
- <iostream>
- #include<fstream>
- usingnamespace 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;
- }
- }
![]() |
Similar Threads
- Extracting a field in a txt file (C)
- Using Global Low-Level Hooks Without Using A Dll (Computer Science)
- Scan text file to find all words of 4 characters or less (Shell Scripting)
- help! what does this mean (Viruses, Spyware and other Nasties)
- Norton Antivirus will not scan (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: compiling gcc/c++
- Next Thread: Parents/Children - Processes/Threads
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






