Created
August 27, 2017 04:45
-
-
Save karthikkondagalla/2d7fd3374cb84c35baaf0876d8a67de0 to your computer and use it in GitHub Desktop.
C++ program to remove comments from a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
#include<string> | |
#include "error.h" | |
#include "/home/cs689/common/689.h" | |
/**************************************************************** | |
FUNCTION: error | |
ARGUMENTS: string | |
RETURNS: void | |
NOTES: This prints the error message | |
****************************************************************/ | |
void error(string msg)//this is for printing the error message. | |
{ | |
cerr<<msg<<endl; | |
exit(EXIT_FAILURE); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TEST | |
#define TEST | |
#include<iostream> | |
#include<string> | |
#include "/home/cs689/common/689.h" | |
/**************************************************************** | |
FUNCTION: error | |
ARGUMENTS: string | |
RETURNS: void | |
NOTES: This is declaration for the error function which prints error | |
****************************************************************/ | |
void error(string msg); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "/home/cs689/common/689.h" | |
#include <fstream> | |
#include"prog4.h" | |
#include "error.h" | |
using namespace std; | |
/**************************************************************** | |
FUNCTION: open_files() | |
ARGUMENTS: input file stream and output file stream | |
RETURNS: void i.e nothing | |
NOTES: This is used for opening the files and calling process_data() function | |
****************************************************************/ | |
void open_files(ifstream& read,ofstream& write)//Definition for the open_files() | |
{ | |
string infile="prog4d.cc";//reading the file | |
read.open(infile.c_str());//open the read file | |
string outfile="prog4out.cc"; | |
write.open(outfile.c_str());//open the write file | |
process_data(read,write);//calling the process_data() function | |
return; | |
} | |
/**************************************************************** | |
FUNCTION: process_data() | |
ARGUMENTS: input file stream and output file stream | |
RETURNS: void | |
NOTES: Removes the comment line part | |
****************************************************************/ | |
void process_data(ifstream& read,ofstream& write)//Definition for the process_data() | |
{ | |
unsigned int number=0,position,flag1 = 0, flag2 = 0 , flag3 = 0;//I am using flags to check the different conditions we encounter | |
if(read.is_open()) | |
{ | |
while(getline(read,s))//to read the whole file line by line | |
{ | |
unsigned int i; | |
string s1; | |
position = s.size(); | |
flag2 = 0; | |
flag3 = 0; | |
for(i = 0 ;i<s.size(); i++) | |
{ | |
// this checks for the double quotes " | |
if(s[i] == '"' && flag1 == 0 && flag2 == 0 && number == 0) | |
{ | |
flag3=1; | |
number = 1; | |
s1 += '"'; | |
i++; | |
} | |
if(s[i] == '"' && flag1 == 0 && flag2 == 0 && number == 1) | |
{ | |
flag3 = 0; | |
number = 0; | |
s1 += '"'; | |
i++; | |
} | |
//this checks for Double-slash // | |
if(s[i] == '/' && s[i+1] == '/' && flag3 == 0 && flag1 == 0) | |
{ | |
flag2 = 1; | |
position = i; | |
} | |
// This checks for slash-star /* | |
if(s[i] == '/' && s[i+1] == '*' && flag2 == 0 && flag3 == 0) | |
{ | |
flag1 = 1; | |
i += 2; | |
} | |
if(s[i] == '*' && s[i+1] == '/' && flag2 == 0 && flag3 == 0) | |
{ | |
flag1 = 0; | |
i += 2; | |
if(s[i] == '/' && s[i+1] == '*') | |
{ | |
flag1 = 1; | |
} | |
} | |
if(flag1 == 0) | |
{ | |
if(i < position) | |
{ | |
s1 += s[i]; | |
} | |
} | |
} | |
write<<s1<<endl; | |
} | |
} | |
else | |
{ | |
error("Error in opening the file"); | |
} | |
} | |
/**************************************************************** | |
FUNCTION: close_files() | |
ARGUMENTS: input file stream and output file stream | |
RETURNS: void | |
NOTES: Closes the files | |
****************************************************************/ | |
void close_files(ifstream& read,ofstream& write)//Definition for the close_file() function | |
{ | |
read.close();//this is close the read file | |
write.close();//this is close the write file | |
} | |
/**************************************************************** | |
FUNCTION: main | |
RETURNS: Integer | |
NOTES: Here we call the open_file and close_file contents | |
****************************************************************/ | |
int main () | |
{ | |
ifstream read;//variable for the input file | |
ofstream write;//variable for the output file | |
open_files(read,write);//calling the open_files() function | |
close_files(read,write);//calling the close_files() function | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef PROG4_H_INCLUDED | |
#define PROG4_H_INCLUDED | |
#include<fstream> | |
#include<iostream> | |
#include<string> | |
using namespace std; | |
void open_files(ifstream&,ofstream&); | |
void process_data(ifstream&,ofstream&); | |
void close_files(ifstream&,ofstream&); | |
char c1, c2; | |
string s; | |
unsigned int flag, flag2, flag3,position,num; | |
bool isInsideComment; | |
#endif // PROG4_H_INCLUDED |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "/home/cs689/common/689.h" | |
#define DATA_FILE "/home/cs689/progs/p4/prog4d.d" | |
const unsigned MAXWORDS = 1000; | |
const unsigned MAXLEN = 20; | |
const unsigned LINESIZE = 5; | |
unsigned num_words = 0; | |
char word_name [ MAXWORDS ] [ MAXLEN + 1 ]; | |
char prog_name1 [ ] = "// Begin Program Data //"; | |
char prog_name2 [ ] = "/* End Program Data */"; | |
void get_words ( ); | |
void print_words ( ); | |
void clean_entry ( char [ ], char [ ] ); | |
int main ( ) | |
{ | |
get_words ( ); | |
print_words ( ); | |
return 0; | |
} | |
void get_words ( ) | |
{ | |
int c; unsigned n = 0; | |
char entry [ MAXLEN + 1 ], save [ MAXLEN + 1 ]; | |
FILE* fptr = fopen ( DATA_FILE, "r" ); | |
while ( fscanf ( fptr, "%20s", entry ) != EOF ) { | |
if ( strlen ( entry) == MAXLEN ) { | |
while ( !isspace ( c = cin.get ( ) && c != EOF ) ); | |
if ( c == EOF ) break; | |
} | |
clean_entry ( entry, save ); | |
if ( !strlen ( save ) ) continue; | |
if ( ++n > MAXWORDS ) break; | |
assert ( strcpy ( word_name [ num_words++ ], save ) ); | |
} | |
fclose ( fptr ); | |
} | |
void clean_entry ( char e [ ], char s [ ] ) | |
{ | |
int i, j; | |
char c; | |
for ( i = 0; ( c = e [ i ] ) && !isalnum ( c ); i++ ); | |
for ( j = i; ( c = e [ j ] ) && isalnum ( c ); j++ ); | |
strcpy ( s, e + i ); | |
s [ j - i ] = 0; | |
} | |
void print_words ( ) | |
{ | |
cout << prog_name1 << endl; | |
cout << "Number of words: " << num_words << endl << endl; | |
for ( unsigned i = 0; i < num_words; i++ ) { | |
cout << " " << word_name [ i ]; | |
if ( i % LINESIZE == LINESIZE - 1 || i == num_words - 1 ) | |
cout << endl; | |
} | |
cout << endl << prog_name2 << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment