Holey_yan

[C++] DeleteComment

Oct 17th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.00 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(int argc, char const *argv[])
  5. {
  6.     FILE *fin  = fopen("In.cpp", "r");
  7.     FILE *fout = fopen("Out.cpp", "w");
  8.     bool IS_COMMENT = false;
  9.     char readChar;
  10.     while ((readChar = fgetc(fin)) != EOF) {
  11.         if (IS_COMMENT) {
  12.             if (readChar != '*') {
  13.                 // Do Nothing
  14.             } else {
  15.                 char nextChar = fgetc(fin);
  16.                 if (nextChar == '/') {
  17.                     IS_COMMENT = false;
  18.                 }
  19.                 else {
  20.                     putc(readChar, stdout); putc(nextChar, stdout); // Debug
  21.                     fputc(readChar, fout); fputc(nextChar, fout);
  22.                 }
  23.             }
  24.         } else {
  25.             if (readChar != '/') {
  26.                 putc(readChar, stdout);
  27.                 fputc(readChar, fout);
  28.             } else {
  29.                 char nextChar = fgetc(fin);
  30.                 if (nextChar == '*') {
  31.                     IS_COMMENT = true;
  32.                 } else {
  33.                     putc(readChar, stdout); putc(nextChar, stdout); // Debug
  34.                     fputc(readChar, fout); fputc(nextChar, fout);
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     fclose(fin); fclose(fout);
  40.     printf("\n /* End of Code, Press Enter to exit. */"); getchar();
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment