S
srenusa
hi, I am quite new to c++ programming and I would need some help with
my college assigment:
write a program which reads input c++ file and removes commentary. The
commentary can be oneline(//) and block commentary(/**/. The name of
the file is read from the command line. use functions and global
variables if neccesary.
here is my code. Thanks in advance
#include <fstream>
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
ifstream fp;
ofstream fpp;
void remove (int kraj, char *line){ //removes '//' and saves to file
int i;
for (i = 0; i <= kraj; i++) {
fpp<<line;
}
}
void proceed (int kkraj, char *line,int lenght){
int i;
for (i = kkraj; i<=lenght; i++) {
fpp<<line;
}
}
void main(){
int i ,k, k2,k3;
char filename[12];
char *line;
cout<<"what is the name of the file";
cin>>filename;
fp.open (filename, ios :: in); //file to read from
if (!fp){
cout<<"\n error opening\n";
exit(0);
}
fpp.open("no commentary", ios:ut); //file to save non commentary
program to
if (!fpp){
cout << "Error opening file.\n";
}
while(line=fgets(fp)){
for(i=0; i<strlen(line); i++){
if ((line=='/')&&(line[i+1]=='/')) {
k=i-1;
remove(k, line);
}
if ((line=='/')&&(!begin)&&(line[i+1]=='*')) {
k2=i-1;
remove(k2, line);
begin=1;
}
if ((line=='*')&&(begin)&&(line[i+1]=='/')){
k3=i-1;
begin=0;
proceed(k3, line, strlen(line)) ;
}
}
fp.close() ;
fpp.close() ;
return;
}
}
my college assigment:
write a program which reads input c++ file and removes commentary. The
commentary can be oneline(//) and block commentary(/**/. The name of
the file is read from the command line. use functions and global
variables if neccesary.
here is my code. Thanks in advance
#include <fstream>
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
ifstream fp;
ofstream fpp;
void remove (int kraj, char *line){ //removes '//' and saves to file
int i;
for (i = 0; i <= kraj; i++) {
fpp<<line;
}
}
void proceed (int kkraj, char *line,int lenght){
int i;
for (i = kkraj; i<=lenght; i++) {
fpp<<line;
}
}
void main(){
int i ,k, k2,k3;
char filename[12];
char *line;
cout<<"what is the name of the file";
cin>>filename;
fp.open (filename, ios :: in); //file to read from
if (!fp){
cout<<"\n error opening\n";
exit(0);
}
fpp.open("no commentary", ios:ut); //file to save non commentary
program to
if (!fpp){
cout << "Error opening file.\n";
}
while(line=fgets(fp)){
for(i=0; i<strlen(line); i++){
if ((line=='/')&&(line[i+1]=='/')) {
k=i-1;
remove(k, line);
}
if ((line=='/')&&(!begin)&&(line[i+1]=='*')) {
k2=i-1;
remove(k2, line);
begin=1;
}
if ((line=='*')&&(begin)&&(line[i+1]=='/')){
k3=i-1;
begin=0;
proceed(k3, line, strlen(line)) ;
}
}
fp.close() ;
fpp.close() ;
return;
}
}