Saving memory allocated pointer matrix in binary file

0

I got a sort of database for a library with info about books and i need to dynamically allocate space for it, read user input, save it in an binary file and them retreat it.I successfully saved the qtd variable alone, but the program crashes when i try to save to the file with everything. The compiler gave the error "Unhandled exception at 0x0f6fc9c7 (msvcr100d.dll) in livraria.exe: 0xC0000005: Access violation reading location 0x000000cc." .

    struct livro {
    char titulo [100];//title
    char autor [100];//writer
    float prec;//price
    };

struct livro * cadastro (int *qtd){ //function for user registration of the data
int i;
struct livro *Livros=NULL;


qtdcad:
printf("Insira a quantidade de livros:");
scanf ("\n%i", qtd); //gets quantity of books


if((*qtd>1000)||(*qtd<1)){ //limit the quantity to a value
    goto qtdcad;
printf("quantidade invalida");
}

Livros = malloc( *qtd *sizeof(struct livro)); //allocate memory based on 'qtd'

printf ("insira os dados de cada livro:");

    for (i=0;i<*qtd;i=i+1){
        getchar();
    printf ("\n\n\ninsira o titulo:");
    fgets (Livros[i].titulo,100, stdin); //gets the title


    printf ("\ninsira o nome:");
   fgets (Livros[i].autor,100, stdin);//gets the writer


    printf ("\ninsira o preco :");
    scanf (" %f",&Livros[i].prec,100, stdin);//gets the price


    }
return Livros;
}

    void salvar (int qtd, struct livro *Livros){//function to save
        int i, checkwrt;
        FILE *fp;
    fp= fopen ("Livros_save", "wb" );

    Livros=NULL;
    if ( NULL ==fp){ 
        printf ( "O arquivo não pode ser aberto.\n" );
        return;
    }

    checkwrt=fwrite (&qtd, sizeof(int), 1, fp); //saves the 'qtd' variable in the file to be able later to allocate the correct size for the database 

                if (checkwrt!=1) {
                     printf("erro na escrita\n");
                }

        for(i=0;i,qtd;i=i+1){ //loop to save the lines
            if(Livros[i].titulo){
            checkwrt = fwrite (&Livros[i], sizeof(struct livro), qtd, fp);
                if (checkwrt!=1) {
                printf("erro na escrita\n");
                }
            }
        }

    fclose(fp);
    }

struct livro *recuperar (int *qtd, struct livro *Livros){//function to retreat the data
    int i, checkwrt;
    FILE *fp;
Livros=NULL;
fp= fopen ("Livros_save", "rb" );


    if ( NULL ==fp)
    { 
    printf ( "O arquivo não pode ser aberto.\n" );
    return NULL;
    }

    checkwrt = fread(qtd, sizeof(int),1,fp); //reads 'qtd'in the file and pass it to the variable
            if(checkwrt!=1){
            printf("erro na escrita\n");
            }



    for(i=0;i<*qtd;i=i+1){
        if(Livros[i].titulo){
        checkwrt=fread(&Livros[i], sizeof(struct livro),*qtd,fp); //reads the lines and pass it to the matrix
            if(checkwrt!=1){
                if ( feof (fp) ){
                    break;
                }
            printf("erro na escrita\n");
            }
        }
c
pointers
malloc
binaryfiles
asked on Stack Overflow Jan 1, 2015 by Zeor137

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0