1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
void openFile(ifstream& in, ofstream& out); //opens an input and output file
void removeComments(ifstream& in, ofstream& out); //removes comments from a file
int main(void)
{
ifstream source;
ofstream target;
openFile(source, target);
removeComments(source, target);
cout<<"The output file now has no comments in it while your input file is unchanged.";
source.close(); /* Close the files I opened */
target.close();
cin.ignore(1000, '\n'); //keeps the window open
return 0;
}/*endl of main*/
/******************************************************
* opepns an input and output file
*
*******************************************************/
void openFile(ifstream& in, ofstream& out)
{/*beginning of openFile */
char myAnser = 'N';
char anser;
string infile, outfile;
cout<<"Please enter the file name you would like the comments removed from: "; /* Infile to have comments removed */
cin >> infile;
cout<<"Please enter the file name where you would like the file without comments to go: "; /* Outfile with all the comments removed */
cin >> outfile;
in.open ( infile.c_str() ); /* Returns a constant char array containing the characters stored in infile, stopped by a null character. */
out.open ( outfile.c_str() ); /* Returns a constant char array containing the characters stored in outfile, stopped by a null character. */
//checks for valid input
while(in.fail())
{/*beginning of while loop*/
cout << "The file you specified failed to open" << endl;
cout << "Do you wan't to continue [y/n]" << endl;
cin >> anser;
anser = toupper(anser);
if (anser == 'N')
exit(0);
else if (anser == 'Y')
{
in.close();
cout<<"Please enter the file name you would like the comments removed from: "; /* Infile to have comments removed */
cin >> infile;
cout<<"Please enter the file name where you would like the file without comments to go: "; /* Outfile with all the comments removed */
cin >> outfile;
in.open ( infile.c_str() ); /* Returns a constant char array containing the characters stored in infile, stopped by a null character. */
out.open ( outfile.c_str() ); /* Returns a constant char array containing the characters stored in outfile, stopped by a null character. */
}
else
cout << "Invalid input" << endl;
}/*end of while loop*/
}/*end of function openFile*/
/*******************************************************
* removes comments from a file
*
********************************************************/
void removeComments(ifstream& inFile, ofstream& outFile)
{
char firstChar, secondChar;
string discard;
int index;
while ( ! inFile.eof() ) /* While loop to make sure the whole input file is read */
{
//finds the first occurance of / character
firstChar = inFile.get();
if (firstChar == '\n')
outFile << firstChar;
else if (firstChar == '/')
{
//determins if this position is the beginning of a comment
secondChar = inFile.get();
if ((firstChar == '/') && (secondChar == '/'))
{
getline(inFile, discard);
}
else if ((firstChar == '/') && (secondChar == '*'))
{
while (firstChar != '*' && secondChar != '/')
{
firstChar = inFile.get();
if (firstChar == '*')
{
secondChar = inFile.get();
}
else if (secondChar == '/')
{
break;
}
}/*end of inner while loop*/
}
}
else
outFile << firstChar;
}/*end of outer while loop*/
}/*end of function removeComments*/
|