Advertisement
tomasaccini

Untitled

Jul 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. enum estados {  FUERA_COMENTARIO,
  5.                 BARRA_OPEN,
  6.                 DENTRO_COMENTARIO,
  7.                 ASTERISCO_CLOSE
  8.             };
  9.  
  10. void escribir_byte(FILE* f, int* seek_escritura, char c) {
  11.     fseek(f, *seek_escritura, SEEK_SET);
  12.     fputc(c, f);
  13.     *seek_escritura = ftell(f);
  14. }
  15.  
  16. char leer_byte(FILE* f, int* seek_lectura) {
  17.     fseek(f, *seek_lectura, SEEK_SET);
  18.     char c = fgetc(f);
  19.     *seek_lectura = ftell(f);
  20.     return c;
  21. }
  22.  
  23. int main() {
  24.     FILE* f = fopen("fuente.c", "r+");
  25.     int seek_lectura = 0;
  26.     int seek_escritura = 0;
  27.     enum estados estado_actual = FUERA_COMENTARIO;
  28.     char c;
  29.     while ( (c = leer_byte(f, &seek_lectura)) != EOF ){
  30.         if (estado_actual == FUERA_COMENTARIO) {
  31.             if (c == '/') {
  32.                 estado_actual = BARRA_OPEN;
  33.             } else {
  34.                 escribir_byte(f, &seek_escritura, c);
  35.             }
  36.         } else if (estado_actual == BARRA_OPEN)  {
  37.             if (c == '*') {
  38.                 estado_actual = DENTRO_COMENTARIO;
  39.             } else {
  40.                 estado_actual = FUERA_COMENTARIO;
  41.                 escribir_byte(f, &seek_escritura, '/');
  42.                 escribir_byte(f, &seek_escritura, c);
  43.             }
  44.         } else if (estado_actual == DENTRO_COMENTARIO)  {
  45.             if (c == '*') {
  46.                 estado_actual = ASTERISCO_CLOSE;
  47.             }
  48.         } else if (estado_actual == ASTERISCO_CLOSE)  {
  49.             if (c == '/') {
  50.                 estado_actual = FUERA_COMENTARIO;
  51.             } else {
  52.                 estado_actual = DENTRO_COMENTARIO;
  53.             }
  54.         }
  55.     }
  56.     fclose(f);
  57.     truncate("fuente.c", seek_escritura);
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement