Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- int main()
- {
- char *program = 0, *modified_program = 0;
- int file_length = 0, i = 0, j = 0;
- bool s_cmt = false;
- bool m_cmt = false;
- FILE *fp = fopen("input.c", "r");
- FILE *fp2 = fopen("output.c", "w");
- if (!fp)
- {
- printf("Source can't be opened");
- exit(-1);
- }
- fseek(fp, 0, SEEK_END);
- file_length = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- program = (char *)malloc(file_length + 1);
- modified_program = (char *)malloc(file_length + 1);
- fread(program, sizeof(char), file_length, fp);
- program[file_length] = '\0';
- printf("Before removing comment:\n");
- for (i = 0; i < file_length; i++)
- {
- printf("%c", program[i]);
- }
- for (int i = 0; i < file_length; i++)
- {
- if (program[i] == '/' && program[i + 1] == '/')
- {
- s_cmt = true;
- i++;
- }
- else if (program[i] == '/' && program[i + 1] == '*')
- {
- m_cmt = true;
- i++;
- }
- else if (s_cmt == true && program[i] == '\n')
- {
- s_cmt = false;
- }
- else if (m_cmt == true && program[i] == '*' && program[i + 1] == '/')
- {
- m_cmt = false;
- i++;
- }
- else if (s_cmt || m_cmt)
- continue;
- else
- {
- modified_program[j] = program[i];
- j++;
- }
- }
- modified_program[j] = '\0';
- fputs(modified_program, fp2);
- printf("\n\n\n\nAfter removing comment:\n");
- fputs(modified_program, stdout);
- fclose(fp);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement