Thread: Problem reading a database from a file using pointers

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    101

    Problem reading a database from a file using pointers

    Hi, I hope someone can clarify me how can I read from a file using a pointer that I need to read all the pets that I am adding through a function. Before reading from a file, everything worked perfectly, I was able to print all the records I added previously. I am not able to do so with files. This is my code:

    Structure:

    Code:
    typedef struct {
        unsigned int dia;
        unsigned int mes;
        unsigned int anio;
    } fecha;
    
    
    typedef struct mascota {
        char nombre_mascota[100];
        char tipo_mascota[100];
        char raza[100];
        char sexo[100];
        char observaciones[1024];
        char dolencia[1024];
        fecha nacimiento;
        struct mascota *siguiente;
    } tmascota;
    
    
    tmascota *primero=(tmascota*) NULL;
    tmascota *ultimo=(tmascota*) NULL;
    The function to print all the records added:

    Code:
    void listar_mascotas(tmascota *ptr) {
    
    
        FILE *file;
        int cont=0;
        ptr=primero;
        file=fopen("gestion_veterinaria.dat", "rb");
        int num_leidos=1;
        while (ptr!=NULL && num_leidos) {
            num_leidos=fread(ptr, sizeof(tmascota),1, file);
            if (num_leidos) {
                printf("\n\n%s \nNombre de la Mascota: %s \nFecha de Nacimiento: %d/%d/%d\nTipo: %s \n"
                       "Raza: %s \nSexo: %s \nObservaciones: %s \nDolencia: %s\n",
                       ptr->nombre_mascota, ptr->nombre_mascota, ptr->nacimiento.dia, ptr->nacimiento.mes, 
                       ptr->nacimiento.anio, ptr->tipo_mascota, ptr->raza, ptr->sexo, ptr->observaciones, ptr->dolencia);
            ptr=ptr->siguiente;
            cont++;
            }
        }
        fclose(file);
    }
    I have problems with: num_leidos=fread(ptr, sizeof(tmascota),1, file);

    Can someone see what is wrong? As I said, before file management everything worked perfectly.

  2. #2
    Registered User
    Join Date
    May 2017
    Posts
    101
    ok, I have an update I made this changes:

    Code:
    typedef struct {
        unsigned int dia;
        unsigned int mes;
        unsigned int anio;
    } fecha;
    
    
    
    
    typedef struct mascota {
        char nombre_mascota[100];
        char tipo_mascota[100];
        char raza[100];
        char sexo[100];
        char observaciones[1024];
        char dolencia[1024];
        fecha nacimiento;
        struct mascota *siguiente;
    } tmascota;
    
    
    tmascota var;
    tmascota *primero=(tmascota*) NULL;
    tmascota *ultimo=(tmascota*) NULL;
    Code:
    void listar_mascotas(tmascota *ptr) {
    
    
        FILE *file;
        int cont=0;
        ptr=primero;
        file=fopen("gestion_veterinaria.dat", "rb");
        int num_leidos=1;
        while (ptr!=NULL && num_leidos) {
            num_leidos=fread(var, sizeof(tmascota),1, file);
            if (num_leidos) {
                printf("\n\n%s \nNombre de la Mascota: %s \nFecha de Nacimiento: %d/%d/%d\nTipo: %s \n"
                       "Raza: %s \nSexo: %s \nObservaciones: %s \nDolencia: %s\n",
                       ptr->nombre_mascota, ptr->nombre_mascota, ptr->nacimiento.dia, ptr->nacimiento.mes, 
                       ptr->nacimiento.anio, ptr->tipo_mascota, ptr->raza, ptr->sexo, ptr->observaciones, ptr->dolencia);
            ptr=ptr->siguiente;
            cont++;
            }
        }
        fclose(file);
    }
    I added tmascota var; and in fwrite and fread: var. At least compiles but only display the first element added in the list. Why?

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    101
    Never mind, solved by myself, intnum_leidos=1; num_leidos= removed and substituted by &primero. Now prints all the records. You can ignore this post.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This makes no sense at all.

    > num_leidos=fread(var, sizeof(tmascota),1, file);
    You do realise that you're also reading the siguiente field (which is a pointer) directly from the file.

    So what does this do?
    ptr=ptr->siguiente;

    TBH, the only chance this code has of working is if you populate your list, save it to a file and then immediately load it in again.

    If you just run the code and load a previously saved file, then I'd expect it to just blow up in your face.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2017
    Posts
    101
    Quote Originally Posted by Salem View Post
    This makes no sense at all.

    > num_leidos=fread(var, sizeof(tmascota),1, file);
    You do realise that you're also reading the siguiente field (which is a pointer) directly from the file.

    So what does this do?
    ptr=ptr->siguiente;

    TBH, the only chance this code has of working is if you populate your list, save it to a file and then immediately load it in again.

    If you just run the code and load a previously saved file, then I'd expect it to just blow up in your face.
    Yes, I removed num_leidos, took the idea of one of my classes. This is exactly what I am doing, saving and loading the file in every function. After a hard work I will have the chance to submit the project, I don't expect a 10/10 but at least a 5/10 to pass it.

    This course will end the next week but I am still interested in learning C/C++. I will come back again soon to ask you advice to do so.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reading from file... problem with strings
    By Ruben Marques in forum C Programming
    Replies: 58
    Last Post: 07-02-2014, 03:23 PM
  2. Reading a file into an array of pointers
    By atac in forum C Programming
    Replies: 2
    Last Post: 03-24-2013, 09:55 PM
  3. using SqlTransaction when reading database
    By KIBO in forum C# Programming
    Replies: 1
    Last Post: 07-30-2012, 09:09 AM
  4. Parsing a csv file crashing reading array of pointers
    By slackwarefan in forum C Programming
    Replies: 2
    Last Post: 07-23-2010, 01:23 AM
  5. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM

Tags for this Thread