Advertisement
tomasaccini

Untitled

Jul 22nd, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5.  
  6. bool leer_numero(FILE* f, int* seek_lectura, int16_t* valor) {
  7.     fseek(f, *seek_lectura, SEEK_SET);
  8.     char aux[2];
  9.     if ( fread(aux, 2, 1, f) == 0 )
  10.         return false;
  11.     memcpy(valor, aux, 2);
  12.     *seek_lectura = ftell(f);
  13.     return true;
  14. }
  15.  
  16. void escribir_numero(FILE* f, int* seek_escritura, int16_t valor) {
  17.     fseek(f, *seek_escritura, SEEK_SET);
  18.     fwrite(&valor, 2, 1, f);
  19.     *seek_escritura = ftell(f);
  20. }
  21.  
  22. int main() {
  23.     FILE* f = fopen("a.bin", "r+");
  24.     if (!f) return -1;
  25.     int seek_lectura = 0;
  26.     int seek_escritura = 0;
  27.     size_t pos = 0;
  28.     int16_t valor;
  29.     while ( leer_numero(f, &seek_lectura, &valor) ) {
  30.         valor += pos;
  31.         ++pos;
  32.         escribir_numero(f, &seek_escritura, valor);
  33.     }
  34.     fclose(f);
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement