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