Advertisement
tomasaccini

Untitled

Jul 23rd, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5.  
  6. bool es_digito(char c) {
  7. return (c >= '0' && c <= '9');
  8. }
  9.  
  10. bool es_letra(char c) {
  11. return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'));
  12. }
  13.  
  14. char leer_byte(FILE* f, int* seek_lectura) {
  15. fseek(f, *seek_lectura, SEEK_SET);
  16. char c = fgetc(f);
  17. *seek_lectura = ftell(f);
  18. return c;
  19. }
  20.  
  21. void escribir_byte(FILE* f, int* seek_escritura, char c) {
  22. fseek(f, *seek_escritura, SEEK_SET);
  23. fputc(c, f);
  24. *seek_escritura = ftell(f);
  25. }
  26.  
  27. bool validar_siguientes(FILE* f, int* seek_lectura) {
  28. char c;
  29. for (size_t i = 0; i < 2; i++) {
  30. if ( (c = leer_byte(f, seek_lectura)) == EOF || !es_digito(c))
  31. return false;
  32. }
  33. for (size_t i = 0; i < 4; i++) {
  34. if ( (c = leer_byte(f, seek_lectura)) == EOF || !es_letra(c))
  35. return false;
  36. }
  37. return true;
  38. }
  39.  
  40. int main() {
  41. FILE* f = fopen("a.bin", "r+");
  42. if (!f) return -1;
  43. int seek_lectura = 0;
  44. int seek_escritura = 0;
  45. char c;
  46. while ( (c = leer_byte(f, &seek_lectura)) != EOF ) {
  47. int seek_actual = seek_lectura;
  48. --seek_lectura;
  49. fseek(f, seek_lectura, SEEK_SET);
  50. if (!validar_siguientes(f, &seek_lectura)) {
  51. fseek(f, seek_actual, SEEK_SET);
  52. printf("%c", c);
  53. escribir_byte(f, &seek_escritura, c);
  54. }
  55. }
  56. fclose(f);
  57. truncate("a.bin", seek_escritura);
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement