Advertisement
tomasaccini

Untitled

Jul 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3.  
  4. char leer_byte(FILE* f, int* seek_lectura) {
  5.     fseek(f, *seek_lectura, SEEK_SET);
  6.     char c = fgetc(f);
  7.     *seek_lectura = ftell(f);
  8.     return c;
  9. }
  10.  
  11. char leer_byte_reversa(FILE* f, int* seek_lectura) {
  12.     fseek(f, *seek_lectura, SEEK_SET);
  13.     char c = fgetc(f);
  14.     if (ftell(f) == 1) {
  15.         fseek(f, 0, SEEK_SET);
  16.         *seek_lectura = -1;
  17.     } else {
  18.         fseek(f, -2, SEEK_CUR);
  19.         *seek_lectura = ftell(f);
  20.     }
  21.     return c;
  22. }
  23.  
  24. void escribir_byte(FILE* f, int* seek_escritura, char c) {
  25.     fseek(f, *seek_escritura, SEEK_SET);
  26.     fputc(c, f);
  27.     *seek_escritura = ftell(f);
  28. }
  29.  
  30. void escribir_byte_reversa(FILE* f, int* seek_escritura, char c) {
  31.     fseek(f, *seek_escritura, SEEK_SET);
  32.     fputc(c, f);
  33.     if (ftell(f) == 1) {
  34.         fseek(f, 0, SEEK_SET);
  35.         *seek_escritura = -1;
  36.     } else {
  37.         fseek(f, -2, SEEK_CUR);
  38.         *seek_escritura = ftell(f);
  39.     }
  40. }
  41.  
  42.  
  43. void desplazar_derecha(const char * file_name, long int offset) {
  44.     FILE* f = fopen(file_name, "r");
  45.     fseek(f, 0, SEEK_END);
  46.     int seek_lectura = ftell(f) - 1; // Me paro en la ultima letra
  47.     int seek_escritura = seek_lectura + offset;
  48.     fclose(f);
  49.     truncate(file_name, seek_escritura);
  50.     f = fopen(file_name, "r+");
  51.  
  52.     while (seek_lectura >= 0) {
  53.         char c = leer_byte_reversa(f, &seek_lectura);
  54.         escribir_byte_reversa(f, &seek_escritura, c);
  55.     }
  56.     for (int i = 0; i < offset; i++){
  57.         escribir_byte_reversa(f, &seek_escritura, '_');
  58.     }
  59.     fclose(f);
  60. }
  61.  
  62. void desplazar_izquierda(const char * file_name, long int offset) {
  63.     FILE* f = fopen(file_name, "r+");
  64.     int seek_escritura = 0;
  65.     int seek_lectura = offset * -1;
  66.  
  67.     fseek(f, 0, SEEK_END);
  68.     size_t tam = ftell(f);
  69.     fseek(f, 0, SEEK_SET);
  70.  
  71.     while (seek_lectura < tam) {
  72.         char c = leer_byte(f, &seek_lectura);
  73.         escribir_byte(f, &seek_escritura, c);
  74.     }
  75.     fclose(f);
  76.     truncate(file_name, tam + offset);
  77. }
  78.  
  79. void desplazar(const char * file_name, long int offset) {
  80.     if (offset > 0)
  81.         desplazar_derecha(file_name, offset);
  82.     else if (offset < 0)
  83.         desplazar_izquierda(file_name, offset);
  84. }
  85.  
  86. int main(){
  87.     desplazar("a.bin", -4);
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement