Why is not the binary file modified?

0

I have to create a program that modifies the age of a binary file, in addition to other sections but I can not understand why this age is not modified, here I attach the code, in the last printf of opc1, I get it perfect but at re-listing the file has not been modified. Grcias!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct datosCliente
{
char nif[10]; //NIF del cliente
char nombre[20]; //nombre
int edad; //edad
};

struct datosCompra
{
char nif[10]; //NIF del cliente
char nombreProducto[20]; //nombre del producto comprado
float precio; //precio
};

void opc1();
int buscar(struct datosCliente *cliente, char nombre[]);
void listar(struct datosCliente cliente, struct datosCompra compra);
int menu();

int main(){

    int opc;
    struct datosCliente cliente;
    struct datosCompra compra;
    do{
        opc=menu();
        switch(opc){
            case 1:
                listar(cliente, compra);
                break;
            case 2:
                opc1();
                break;
            case 3:

                break;
        }
    }while(opc!=4);




    return 0;
}


int menu(){

    int opc;

    printf("1.Listar 2.Modificar 3. Fichero\n");
    scanf("%d", &opc);
    return opc;
}

int buscar(struct datosCliente *cliente, char nombre[]){
    FILE *f;
    long int posicion;
    f=fopen("cliente.bin", "rb");
    if(f==NULL){
        return -2;
    }
    while(fread(cliente, sizeof(struct datosCliente),1,f)){
        printf("HOLA");
        if(strcmp(nombre, cliente->nif)==0){
            printf("HOLA");
            posicion=ftell(f)-sizeof(struct datosCliente);
            fclose(f);
            return posicion;
        }
    }
    fclose(f);

    return -1;

}

void listar(struct datosCliente cliente, struct datosCompra compra){
    FILE *f, *f2;

    f=fopen("cliente.bin", "rb");
    f2=fopen("compra.bin","rb");
    if(f==NULL || f2==NULL){
        printf("Error de apertura.\n");
        return;
    }
    while(fread(&cliente, sizeof(struct datosCliente),1,f)){
        printf("Nombre: %s, Nif: %s, Edad: %d\n", cliente.nombre,cliente.nif, cliente.edad);
    }
    fclose(f);
    while(fread(&compra, sizeof(struct datosCompra), 1,f2)){
        printf("Nombre: %s, Nif: %s, Precio: %d\n", compra.nombreProducto, compra.nif,compra.precio);
    }
    fclose(f2);
}

void opc1(){
    struct datosCliente cliente;
    FILE *f;
    char nif[10];
    long int posicion;
    int aux;
    int edad;
    printf("Que Nif buscas: \n");
    fflush(stdin);
    gets(nif);

    posicion=buscar(&cliente, nif);

    if(posicion!=-1 && posicion!=-2){
        f=fopen("cliente.bin", "ab");

        fseek(f, posicion,SEEK_SET);
        aux=cliente.edad;
        printf("Introduce la edad nueva: \n");
        scanf("%d", &edad);
        edad=cliente.edad;
        fwrite(&cliente, sizeof(struct datosCliente), 1, f);
        printf("El cliente %s tenia %d edad y ahora tiene %d", cliente.nombre, aux, cliente.edad);
    }
    else if (posicion==-1) printf("No se ha encontrado la persona inscrita");
    else printf("Error de apertura.\n");
}
    
asked by Guillermo Fuentes 16.03.2018 в 18:38
source

0 answers