Last active
August 2, 2017 17:35
-
-
Save colltoaction/901c2a66209bf51387a7a193c8e3ba83 to your computer and use it in GitHub Desktop.
Archivos de ejemplo de edición in-place en C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Ejercicio [5] final 12/12/06 | |
* Escribir un algoritmo ANSI C que, sin crear archivos intermedios | |
* altere el archivo a.txt reemplazando la secuencia '//' por '*' | |
* Excepto si se encuentra entre parentesis | |
*/ | |
/* | |
* Compilar con gcc -ansi -std=gnu99 -o prueba inplace-acortar.c | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
int leer(FILE* f, long int* seek) | |
{ | |
fseek(f, *seek, SEEK_SET); | |
int out = fgetc(f); | |
*seek = ftell(f); | |
return out; | |
} | |
void escribir(FILE* f, char c, long int* seek) | |
{ | |
fseek(f, *seek, SEEK_SET); | |
fputc(c, f); | |
*seek = ftell(f); | |
} | |
int main(int argc, char* argv[]) | |
{ | |
FILE *f; | |
int c = 0; | |
long int c_final = 0; | |
long int seek_lectura = 0; | |
long int seek_escritura = 0; | |
int estado = 0; | |
f = fopen("a.txt","r+"); | |
while ((c = leer(f, &seek_lectura)) != EOF) | |
{ | |
c_final++; | |
if (estado == 0) // inicial | |
{ | |
if (c == '/') | |
{ | |
estado = 2; | |
} | |
else | |
{ | |
escribir(f, c, &seek_escritura); | |
if (c == '(') | |
{ | |
estado = 1; | |
} | |
} | |
} | |
else if (estado == 1) // paréntesis | |
{ | |
escribir(f, c, &seek_escritura); | |
if (c == ')') | |
{ | |
estado = 0; | |
} | |
} | |
else if (estado == 2) // hay primera barra | |
{ | |
if (c == '/') | |
{ | |
c_final--; | |
escribir(f, '*', &seek_escritura); | |
estado = 0; | |
} | |
else | |
{ | |
escribir(f, '/', &seek_escritura); | |
escribir(f, c, &seek_escritura); | |
estado = c == '(' ? 1 : 0; | |
} | |
} | |
} | |
fclose(f); | |
truncate("a.txt", sizeof(char) * c_final); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Ejercicio [4] final 20/02/07 | |
* Escribir un algoritmo ANSI C que, sin crear archivos intermedios | |
* altere el archivo a.bin reemplazando la secuencia '-' por '--' | |
*/ | |
/* | |
* Compilar con gcc -ansi -std=gnu99 -o prueba inplace-agrandar.c | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
int leer_reversa(FILE* f, long int* seek) | |
{ | |
fseek(f, *seek, SEEK_SET); | |
int out = fgetc(f); | |
if (*seek == 0) | |
{ | |
*seek = -1; | |
} | |
else | |
{ | |
fseek(f, -2, SEEK_CUR); | |
*seek = ftell(f); | |
} | |
return out; | |
} | |
void escribir_reversa(FILE* f, char c, long int* seek) | |
{ | |
fseek(f, *seek, SEEK_SET); | |
fputc(c, f); | |
if (*seek == 0) | |
{ | |
*seek = -1; | |
} | |
else | |
{ | |
fseek(f, -2, SEEK_CUR); | |
*seek = ftell(f); | |
} | |
} | |
int main(int argc, char* argv[]) | |
{ | |
FILE *f; | |
int c = 0; | |
long int c_final = 0; | |
long int seek_lectura = 0; | |
long int seek_escritura = 0; | |
int estado = 0; | |
f = fopen("a.txt","r"); | |
while ((c = fgetc(f)) != EOF) | |
{ | |
c_final++; | |
if (c == '-') | |
{ | |
c_final++; | |
} | |
} | |
ungetc(c, f); | |
seek_lectura = ftell(f); | |
fclose(f); | |
truncate("a.txt", sizeof(char) * c_final); | |
f = fopen("a.txt","r+"); | |
fseek(f, 0, SEEK_END); | |
// seek_lectura está en la última posición con un caracter válido | |
seek_escritura = ftell(f); | |
while (seek_lectura >= 0) | |
{ | |
c = leer_reversa(f, &seek_lectura); | |
escribir_reversa(f, c, &seek_escritura); | |
if (c == '-') | |
{ | |
escribir_reversa(f, c, &seek_escritura); | |
} | |
} | |
fclose(f); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment