在打印结构时,fprintf()在“ at”模式下的问题

I have a problem when executing my code, at certain point the program ends when there's still code to run. I have a struct called Cliente, I allocate memory for it at the end of the case in order to save information into a file. The part of retrieving data works fine, the problem comes when it has to save the information into the file via fprintf(). Thank you in advance.

Here is the struct Cliente

typedef struct {
    char  *dni;
    char *nombre;
    Fecha fechaNac;
    char *direccion;
    char *telefono;
}Cliente;

这是我试图使用菜单输入运行的代码片段:

Cliente *c;

case '2':
        c = (Cliente*) malloc(sizeof(Cliente)); //Reservo memoria
        fInfoCliente = fopen("..\\Cliente\\InfoClientes.txt", "a");
        fflush(stdin);

        if (fInfoCliente == NULL) {
            printf("\nError al abrir el archivo\n\n");
            menuCliente();
        } else {
            printf("Introduzca la siguiente informacion sobre el cliente:\n");
            printf("\nNombre: ");
            gets(c->nombre);
            printf("DNI: ");
            gets(c->dni);
            printf("Fecha de Nacimiento (DDMMAAAA): ");
            scanf("%i/%i/%i", &c->fechaNac.dia, &c->fechaNac.mes, &c->fechaNac.anyo);

            fflush(stdin);
            printf("Direccion: ");
            gets(c->direccion);
            printf("Telefono: ");
            gets(c->telefono);


            printf("Antes si");
            fprintf(fInfoCliente, "%s", c->nombre);//, c->dni, c->fechaNac.dia, c->fechaNac.mes, c->fechaNac.anyo, c->direccion, c->telefono); ,%s,%i,%i,%i,%s,%s
            printf("Despues no");

            printf("\nDesea añadir otro cliente? (Y/n)");
            scanf("%c", opc);

            fclose(fInfoCliente);
            free(c);
        }
        break;
评论
  • 已注销
    已注销 回复

    You are not allocating any memory for those strings you're reading in. All the char* members of the structure you allocated are just uninitialized pointers.

    Also fflush(stdin) is a bad idea.