tomasaccini

Untitled

Jul 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. /*
  2. Escriba una aplicación C que modifique el archivo a.dat eliminando los bytes
  3. mayores a 0xC3.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <unistd.h>
  8.  
  9. int leer_byte(FILE* f, int* seek_lectura) {
  10.     fseek(f, *seek_lectura, SEEK_SET);
  11.     int c = fgetc(f);
  12.     *seek_lectura = ftell(f);
  13.     return c;
  14. }
  15.  
  16. void escribir_byte(FILE* f, int* seek_escritura, unsigned char c) {
  17.     fseek(f, *seek_escritura, SEEK_SET);
  18.     fputc(c, f);
  19.     *seek_escritura = ftell(f);
  20. }
  21.  
  22. int main() {
  23.     int seek_lectura = 0;
  24.     int seek_escritura = 0;
  25.     FILE* f = fopen("a.dat", "r+");
  26.     if (!f) return -1;
  27.     int c;
  28.     unsigned char character;
  29.     while ( (c = leer_byte(f, &seek_lectura)) != EOF ) {
  30.         character = (unsigned char)c;
  31.         if (character <= 0xC3) {
  32.             escribir_byte(f, &seek_escritura, character);
  33.             printf("DEBUG: lo escribo\n");
  34.         } else {
  35.             printf("DEBUG: no lo escribo\n");
  36.         }
  37.     }
  38.     fclose(f);
  39.     truncate("a.dat", seek_escritura);
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment