Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <stdlib.h>
- bool es_digito(char c) {
- return (c >= '0' && c <= '9');
- }
- bool es_letra(char c) {
- return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
- }
- char leer_byte(FILE* f, int* seek_lectura) {
- fseek(f, *seek_lectura, SEEK_SET);
- char c = fgetc(f);
- *seek_lectura = ftell(f);
- return c;
- }
- void escribir_byte(FILE* f, int* seek_escritura, char c) {
- fseek(f, *seek_escritura, SEEK_SET);
- fputc(c, f);
- *seek_escritura = ftell(f);
- }
- bool validar_siguientes(FILE* f, int* seek_lectura) {
- char c;
- for (size_t i = 0; i < 2; i++) {
- if ( (c = leer_byte(f, seek_lectura)) == EOF || !es_digito(c))
- return false;
- }
- for (size_t i = 0; i < 4; i++) {
- if ( (c = leer_byte(f, seek_lectura)) == EOF || !es_letra(c))
- return false;
- }
- return true;
- }
- int main() {
- FILE* f = fopen("a.bin", "r+");
- if (!f) return -1;
- int seek_lectura = 0;
- int seek_escritura = 0;
- char c;
- while ( (c = leer_byte(f, &seek_lectura)) != EOF ) {
- int seek_actual = seek_lectura;
- --seek_lectura;
- fseek(f, seek_lectura, SEEK_SET);
- if (!validar_siguientes(f, &seek_lectura)) {
- fseek(f, seek_actual, SEEK_SET);
- printf("%c", c);
- escribir_byte(f, &seek_escritura, c);
- }
- }
- fclose(f);
- truncate("a.bin", seek_escritura);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement