Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- bool leer_numero(FILE* f, int* seek_lectura, int16_t* valor) {
- fseek(f, *seek_lectura, SEEK_SET);
- char aux[2];
- if ( fread(aux, 2, 1, f) == 0 )
- return false;
- memcpy(valor, aux, 2);
- *seek_lectura = ftell(f);
- return true;
- }
- void escribir_numero(FILE* f, int* seek_escritura, int16_t valor) {
- fseek(f, *seek_escritura, SEEK_SET);
- fwrite(&valor, 2, 1, f);
- *seek_escritura = ftell(f);
- }
- int main() {
- FILE* f = fopen("a.bin", "r+");
- if (!f) return -1;
- int seek_lectura = 0;
- int seek_escritura = 0;
- size_t pos = 0;
- int16_t valor;
- while ( leer_numero(f, &seek_lectura, &valor) ) {
- valor += pos;
- ++pos;
- escribir_numero(f, &seek_escritura, valor);
- }
- fclose(f);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement