DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   c (http://www.daniweb.com/code/c.html)
-   -   removing comments in c and c++ program (http://www.daniweb.com/code/snippet773.html)

rohit83.ken c syntax
Oct 23rd, 2007
hello frnd this a c program that removes c c++ style comments

  1. //program to print a file by removing comments
  2. /*program*/
  3. #include<stdio.h>
  4. void main(int argc , char *argv[])
  5. {
  6. FILE *fp,*fp1;
  7. char ch;
  8. clrscr();
  9. fp=fopen(argv[1],"r");
  10. fp1=fopen(argv[2],"w");
  11. while(1)
  12. {
  13. ch=fgetc(fp); /*fgetc*/
  14. if(ch==EOF) //eof
  15. break;
  16. else
  17. {
  18. if(ch=='/')
  19. {
  20. ch=fgetc(fp);
  21. if(ch=='/')
  22. {
  23. while(1)
  24. {
  25. ch=fgetc(fp);
  26. if(ch=='\n')
  27. goto label;
  28. }
  29.  
  30. }
  31. if(ch=='*')
  32. {
  33. while(1)
  34. {
  35. ch=fgetc(fp);
  36. if(ch=='*')
  37. {
  38. ch=fgetc(fp);
  39. if(ch=='/')
  40. {
  41. while(1)
  42. {
  43. ch=fgetc(fp);
  44. goto label;
  45. }
  46. }
  47. else printf("*");
  48. }
  49. }
  50. }
  51. else printf("/");
  52. }
  53. }
  54. label:fputc(ch,fp1);
  55. }
  56. fclose(fp); /*closes the file*/
  57. fclose(fp1);
  58. }