Advertisement
zaifmahi

Remove comment and remove space

Feb 13th, 2024
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.80 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include<string.h>
  3. int main() {
  4.     FILE *inputFile = fopen("inp.txt", "r");
  5.     FILE *outputFile = fopen("out.txt", "w");
  6.     if (inputFile == NULL || outputFile == NULL) {
  7.         printf("Unable to open the file.\n");
  8.         return 1;
  9.     }
  10.  
  11.     char currentChar, nextChar;
  12.     while ((currentChar = fgetc(inputFile)) != EOF) {
  13.         if (currentChar == '/') {
  14.             nextChar = fgetc(inputFile);
  15.             if (nextChar == '/') {
  16.                 // Ignore the rest of the line
  17.                 while ((currentChar = fgetc(inputFile)) != '\n' && currentChar != EOF);
  18.             }
  19.             else if (nextChar == '*') {
  20.                 // Ignore until the closing */
  21.                 while (!((currentChar = fgetc(inputFile)) == '*' && (nextChar = fgetc(inputFile)) == '/')) {
  22.                     if (currentChar == EOF) {
  23.                         printf("Error: Unclosed multi-line comment.\n");
  24.                         return 1;
  25.                     }
  26.                 }
  27.             }
  28.             else {
  29.                 fputc(currentChar, outputFile);
  30.                 fputc(nextChar, outputFile);
  31.             }
  32.         }
  33.         else {
  34.             fputc(currentChar, outputFile);
  35.         }
  36.     }
  37.     fclose(inputFile);
  38.     fclose(outputFile);
  39.     FILE* file = fopen("out.txt", "r");
  40.     FILE* file1 = fopen("combined.txt", "w");
  41.  
  42.     char line[256];
  43.         char line1[256];
  44.  
  45.     while (fgets(line, sizeof(line), file)) {
  46.                 if(strlen(line) > 1){
  47.                 fputs(line, file1);
  48.         }
  49.     }
  50.     fclose(file);
  51.         fclose(file1);
  52.         FILE* file2 = fopen("combined.txt", "r");
  53.  
  54.     while(fgets(line1, sizeof(line1), file2)){
  55.                 printf("%s", line1);
  56.     }
  57.     printf("\n");
  58.     fclose(file2);
  59.     return 0;
  60. }
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement