Advertisement
tomasaccini

Untitled

Jul 19th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1. /*
  2. Escribir un programa ISO C que procese el archivo "nros1byte.dat" sobre si
  3. mismo, eliminando los bytes multiplos de 6.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <stdbool.h>
  10.  
  11. char leer_byte(FILE* f, int* seek_lectura) {
  12.     fseek(f, *seek_lectura, SEEK_SET);
  13.     char c = fgetc(f);
  14.     *seek_lectura = ftell(f);
  15.     return c;
  16. }
  17.  
  18. void escribir_byte(FILE* f, int* seek_escritura, char c) {
  19.     fseek(f, *seek_escritura, SEEK_SET);
  20.     fputc(c, f);
  21.     *seek_escritura = ftell(f);
  22. }
  23.  
  24. bool es_multiplo_6(char c){
  25.     return ((c % 6) == 0);
  26. }
  27.  
  28. int main() {
  29.     FILE* f = fopen("nros1byte.dat", "r+");
  30.     if (!f) return -1;
  31.     int seek_lectura = 0;
  32.     int seek_escritura = 0;
  33.     char c;
  34.  
  35.     while ( (c = leer_byte(f, &seek_lectura)) != EOF ) {
  36.         if (!es_multiplo_6(c))
  37.             escribir_byte(f, &seek_escritura, c);
  38.     }
  39.     fclose(f);
  40.     truncate("nros1byte.dat", seek_escritura);
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement