Error file in C

0

I'm doing a simple text file exercise, but it turns out that I miss the message "The program stopped working" and I do not know why, I do not see anything strange. The error jumps to put the year of the date of birth.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct fecha{
    int dia;
    int mes;
    int anio;
}fecha;

struct datosPersonales{
    char DNI[10];
    char nombre[30];
    struct fecha fechaNacimiento;
    char email[30];
}contacto;

//Prototipos de funciones

void escribirDatos(void);
void leerDatos(void);

int main()
{
    printf("\t\tFICHERO EN C\n\n");
    escribirDatos();
    leerDatos();

    return 0;
}

void escribirDatos(void)
{
    FILE *fichero;
    char respuesta[10];

    fichero = fopen("contactos.txt","w");

    if(fichero == NULL)
    {
        printf("Error, el fichero esta vacio o no se ha podido abrir");
        exit(1);
    }
    else
    {
        printf("CONTACTOS\n\n");
        fprintf(fichero,"\tCONTACTOS\n\n");

        do{
            //PEDIR DATOS
            fflush(stdin);
            printf("DNI:");
            gets(contacto.DNI);
            printf("Nombre: ");
            gets(contacto.nombre);
            printf("\nFecha de nacimiento\n");
            printf("Dia: ");
            scanf("%d",&contacto.fechaNacimiento.dia);
            printf("Mes: ");
            scanf("%d",&contacto.fechaNacimiento.mes);
            printf("A%co: ",164);
            scanf("%d",&contacto.fechaNacimiento.anio);
            printf("Correo electronico: ");
            gets(contacto.email);

            //GRABAR EN EL FICHERO
            fprintf(fichero,"DNI = ");
            fprintf(fichero,"%s",contacto.DNI);
            fprintf(fichero,"\nNombre = ");
            fprintf(fichero,"%s",contacto.nombre);
            fprintf(fichero,"\nFecha de nacimiento\n");
            fprintf(fichero,"Dia = ");
            fprintf(fichero,"%s",contacto.fechaNacimiento.dia);
            fprintf(fichero,"\nMes = ");
            fprintf(fichero,"%d",contacto.fechaNacimiento.mes);
            fprintf(fichero,"\nA%co = ",164);
            fprintf(fichero,"%d",contacto.fechaNacimiento.anio);
            fprintf(fichero,"\nCorreo electronico = ");
            fprintf(fichero,"%s",contacto.email);

            printf("Quieres agregar otro contacto?: ");
            scanf("%s",&respuesta);

        }while(strcmpi(respuesta,"si")==0);
    }
    fclose(fichero);
}

void leerDatos(void)
{
    FILE *fichero;
    char caracter;

    fichero = fopen("contactos.txt","r");

    if(fichero == NULL)
    {
        printf("Error, no se han encontrados datos");
        exit(1);
    }
    else
    {
        while(!feof(fichero))
        {
            caracter = fgetc(fichero);
            printf("%c",caracter);
        }
    }
    printf("\n\n");
    fclose(fichero);
}
    
asked by Mario Guiber 02.09.2017 в 18:02
source

1 answer

0

I just realized the failure, at the time of writing in the file, the day of the birth date I was taking it with %s .

Engraving code:

 fprintf(fichero,"DNI = ");
 fprintf(fichero,"%s",contacto.DNI);
 fprintf(fichero,"\nNombre = ");
 fprintf(fichero,"%s",contacto.nombre);
 fprintf(fichero,"\nFecha de nacimiento\n");
 fprintf(fichero,"Dia = ");
 fprintf(fichero,"%d",contacto.fechaNacimiento.dia); //Es '%d' no '%s'
 fprintf(fichero,"\nMes = ");
 fprintf(fichero,"%d",contacto.fechaNacimiento.mes);
 fprintf(fichero,"\nA%co = ",164);
 fprintf(fichero,"%d",contacto.fechaNacimiento.anio);
 fprintf(fichero,"\nCorreo electronico = ");
 fprintf(fichero,"%s",contacto.email);
    
answered by 03.09.2017 / 11:01
source