Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- enum estados { FUERA_COMENTARIO,
- BARRA_OPEN,
- DENTRO_COMENTARIO,
- ASTERISCO_CLOSE
- };
- void escribir_byte(FILE* f, int* seek_escritura, char c) {
- fseek(f, *seek_escritura, SEEK_SET);
- fputc(c, f);
- *seek_escritura = ftell(f);
- }
- char leer_byte(FILE* f, int* seek_lectura) {
- fseek(f, *seek_lectura, SEEK_SET);
- char c = fgetc(f);
- *seek_lectura = ftell(f);
- return c;
- }
- int main() {
- FILE* f = fopen("fuente.c", "r+");
- int seek_lectura = 0;
- int seek_escritura = 0;
- enum estados estado_actual = FUERA_COMENTARIO;
- char c;
- while ( (c = leer_byte(f, &seek_lectura)) != EOF ){
- if (estado_actual == FUERA_COMENTARIO) {
- if (c == '/') {
- estado_actual = BARRA_OPEN;
- } else {
- escribir_byte(f, &seek_escritura, c);
- }
- } else if (estado_actual == BARRA_OPEN) {
- if (c == '*') {
- estado_actual = DENTRO_COMENTARIO;
- } else {
- estado_actual = FUERA_COMENTARIO;
- escribir_byte(f, &seek_escritura, '/');
- escribir_byte(f, &seek_escritura, c);
- }
- } else if (estado_actual == DENTRO_COMENTARIO) {
- if (c == '*') {
- estado_actual = ASTERISCO_CLOSE;
- }
- } else if (estado_actual == ASTERISCO_CLOSE) {
- if (c == '/') {
- estado_actual = FUERA_COMENTARIO;
- } else {
- estado_actual = DENTRO_COMENTARIO;
- }
- }
- }
- fclose(f);
- truncate("fuente.c", seek_escritura);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement