Write a program to remove the C comments(/* */) and C++
comments(//) from a file.
The file should be declared in command line.

Answer Posted / sahil

/*Improved and working answer*/
#include<stdio.h>
void main()
{
FILE *fd;
int ch,flag,i;
char cc;
i=flag=0;
ch=0;
fd = fopen("file","r+");
// ch=fgetc(fd);
while((ch=fgetc(fd))!=EOF)
{ cc=ch;
flag = 0;
if(ch == '/')
{
ch = fgetc(fd);cc =ch;
if(ch == '*')
{
flag = 1;
while(1){
cc = fgetc(fd);
if(fgetc(fd) == '*' && fgetc(fd) == '/')
break;
}
}
else if(ch == '/')
{
flag = 1;
while(fgetc(fd)!= '/');
}

else{
printf("/");
}
}
if(!flag )
printf("%c",ch);
}
fclose(fd);
}

Is This Answer Correct ?    9 Yes 13 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

the constant value in the case label is followed by a a) semicolon b) colon c) braces d) none of the above

718


Why do we use namespace feature?

577


What are the functions to open and close the file in c language?

589


Why does this code crash?

612


What is 'bus error'?

641






What is pointer to pointer in c?

632


Is that possible to add pointers to each other?

893


What is scope rule of function in c?

545


c language supports bitwise operations, why a) 'c' language is system oriented b) 'c' language is problem oriented c) 'c' language is middle level language d) all the above

608


What is the difference between fread buffer() and fwrite buffer()?

667


How can I find out the size of a file, prior to reading it in?

616


int i[2], j; int *pi;i[0] = 1; i[1] = 5; pi = i; j = *pi + 1 + *(pi + 1)Value of j after execution of the above statements will be a) 7 b) 6 c) 4 d) pointer

647


How can I write a function analogous to scanf?

648


Do character constants represent numerical values?

837


What is modeling?

642