removes comments from the given program and prints to the other file

#include<stdio.h>
void main(int argc,char *argv[])
{
	FILE *in,*out;
	
	char ch1,ch2;
	
	int f=0;
	
	in=fopen(argv[1],"r");
	
	out=fopen(argv[2],"w");
	
	while(!feof(in))
	{
		ch1=getc(in);
		
		if(ch1=='/')
		{
			
			if((ch2=getc(in))=='/')
			{
				
				while((ch2=getc(in))!='\n')
				{
					;
				}
			}
			
			else
			
			if(ch2=='*')
			{
				
				while((ch2=getc(in))!=EOF)
				{
					
					if(ch2=='*')
					{
						if((ch2=getc(in))=='/')
						f=1;
					}
					
					if(f==1)
					break;
				}
			}
		}
		
		else
		{
			putc(ch1,out);
		}
	}
	fclose(in);
	fclose(out);
}

removes comments from the given program and prints to the other file

Where's your question? What's the error? Did you not understand Salem's example/guidance? Ask a little more if you didn't; in the same post. No need to make another thread for the same problem.

removes comments from the given program and prints to the other file

#include<stdio.h>
void main(int argc,char *argv[])
{
	FILE *in,*out;
	
	char ch1,ch2;
	
	int f=0;
	
	in=fopen(argv[1],"r");
	
	out=fopen(argv[2],"w");
	
	while(!feof(in))
	{
		ch1=getc(in);
		
		if(ch1=='/')
		{
			
			if((ch2=getc(in))=='/')
			{
				
				while((ch2=getc(in))!='\n')
				{
					;
				}
			}
			
			else
			
			if(ch2=='*')
			{
				
				while((ch2=getc(in))!=EOF)
				{
					
					if(ch2=='*')
					{
						if((ch2=getc(in))=='/')
						f=1;
					}
					
					if(f==1)
					break;
				}
			}
		}
		
		else
		{
			putc(ch1,out);
		}
	}
	fclose(in);
	fclose(out);
}

You're skipping a bunch of characters. For example, if you encounter a "/" and the next character is neither a "/" nor an "*", you do not output it. If you encounter a "/*" and then subsequently encounter an "*" that is not followed by a "/", you likewise do not output it.

Hoppy

You're also

  • using void main(), which is non-standard;
  • using while(!feof(fp)), which reads too far;
  • not checking if your program was passed enough parameters, so argv[1] and argv[2] might not contain file names;
  • not checking if the files could be opened, so your program will crash if this is the case;
  • and trying to stuff the value of getc() into a char, while you need an int (because EOF is an int value, not a char one).

Is / * considered as a start of comment ? I guess no
/* is considered as start of comment

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.