This blog is under construction

Thursday 1 August 2013

C program to eliminate comments from a file

Write a C program to remove comments from a file(C program file).


  #include <stdio.h>
  #include <string.h>
  #define MAX 256

  int main() {
        int ch, i = 0, flag = 0, prev = '\0';
        FILE *fp1, *fp2;
        char fname[MAX], temp[] = "temp.txt";

        /* get the input file name from the user */
        printf("Enter your file name:");
        scanf("%s", fname);

        /* open the input file in read mode */
        fp1 = fopen(fname, "r");

        /* error handling */
        if (!fp1) {
                printf("Unable to open file name!!\n");
                return 0;
        }

        /* open the temporary file in write mode */
        fp2 = fopen(temp, "w");

        /* error handling */
        if (!fp2) {
                printf("Unable to open temporary file!!\n");
                return 0;
        }

        /* removes comments from the given input file */
        prev = fgetc(fp1);
        while ((ch = fgetc(fp1)) != EOF) {

                /* flag is 1 - double slash comment */
                if (flag == 1) {
                        /* skip the contents until you detect \n */
                        if (ch == '\n') {
                                flag = 0;
                                prev = fgetc(fp1);
                        }
                        continue;
                }

                /* flag is 2 - slash arsterix comment */
                if (flag == 2) {
                        /* skip the contents until you detect asterix slash */
                        if (ch == '/' && prev == '*') {
                                flag = 0;
                                prev = fgetc(fp1);
                        }
                        continue;
                }

                /* checking for double slash comment */
                if (ch == '/' && prev == '/') {
                        flag = 1;
                } else if (prev == '/' && ch == '*') {
                        /* slash asterix comment */
                        flag = 2;
                } else {
                        /* contents outside of comments */
                        fputc(prev, fp2);
                }
                prev = ch;
        }

        /* closing the input file */
        fclose(fp1);
        fclose(fp2);

        /* removing the input file */
        remove(fname);

        /* rename the temporary file */
        rename(temp, fname);
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ cat pgm.c 
  /* program to add two numbers */
  #include <stdio.h>
  int main() {
// variable declaration
int a, b, res;
/* sum of two numbers */
res = a + b;
// printing the result
printf("%d", res);

/*
* finding subtraction and division
 * of two numbers here
*/

// subtraction value 
printf("Sub value: %d\n", a - b);

// division value
printf("Div value: %d\n", a / b);
return 0;
    }

  jp@jp-VirtualBox:~/$ ./a.out
  Enter your file name:pgm.c
  jp@jp-VirtualBox:~/$ cat pgm.c 

  #include <stdio.h>
  int main() {
int a, b, res;

res = a + b;
printf("%d", res);


    printf("Sub value: %d\n", a - b);

    printf("Div value: %d\n", a / b);
return 0;
  }





SEE ALSO

    No comments:

    Post a Comment