Jump to content

STRNCPY


Juin
 Share

Recommended Posts

        switch (op){
            case 1:  
                printf("Nao Urgente selecionado.\n");
                      strncpy(dados.urgencia,"Nao Urgente",NCHAR);
                      break;
            case 2: printf("Urgente selecionado.\n");
                      strncpy(dados.urgencia,"Urgente",NCHAR);
                      break;
            case 3: printf("Muito Urgente selecionado.\n");
                    strncpy(dados.urgencia,"Muito Urgente",NCHAR);
                      break;
            default: printf("Tem de seleccionar um numero valido. Escolha de 1, 2 ou 3.\n"); break;
        }

Quando uso o strncpy neste caso como sugeriram-me dá erro no sistema...

Estou a usar correctamente?

Link to comment
Share on other sites

Pois, tens de avaliar consoante o que pretendes, podes aumentar o tamanho do buffer, mas já sabes que se a diferença entre o máximo que puseste no strncpy e o máximo que a string pode comportar der > 0 podes ter o perigo de ter um buffer overflow...

Edit: Se usares a string para muita coisa o teu programa torna-se confuso, foi isso que eu percebi que estavas a querer dizer, se usas strings diferentes, podes consoante a string que fores copiar não usar o NCHAR, aliás aconselho-te a não usar um valor fixo!

here since 2009

Link to comment
Share on other sites

pois afinal o nchar nao faz qualquer diferença, o erro do sistema deve ser da abertura do ficheiro :/

    //EscreverFicheiro(dados,"basedados.txt","m");
    //EscreverProposta(dados,aut,index);



void EscreverProposta(Livro info, int aut, int indx){
    int i;
    printf("Requesitante: %s\n", info.requisitante);
    printf("Data: %d/%d/%d", info.dia, info.mes, info.ano);
    for(i=0; i<aut; i++)
        printf("Autores: %s\n\n", info.autores[i][NCHAR]);
    printf("Titulo: %s",info.titulo[NCHAR]);
    printf("Coleccao: %s\n\n", info.coleccao[NCHAR]);
    printf("Data de Publicacao: %d/%d/%d\n\n", info.diapub, info.mespub, info.anopub);
    printf("Edicao: %d º Edicao\n\n", info.edicao);
    printf("Preco: %.2f €\n\n", info.preco);
    printf("Editor: %s\n\n", info.editor);
    printf("Endereco Editor: %s\n\n", info.EndEditor);
    printf("Numero de Exemplares: %d\n\n", info.NumDeExemplares);
    printf("ISBN/ISSN: %d\n\n", info.ISBN);
    printf("Urgencia: %s\n\n", info.urgencia);
    printf("Chave: %d", indx);
}



void EscreverFicheiro(Livro propostas, char *basedadosf, char *m){
    FILE *f;
    int i;
    f=fopen(basedadosf, m);
    if (f==NULL){
        printf("ERRO DE ABERTURA DO FICHEIRO!\n");
        return;
    }
        fprintf(f,"%s; %d/%d/%d; %s; %d; %d/%d/%d; %d; %.2f; %s; %s; %s\n",
        propostas.requisitante, propostas.dia, propostas.mes, propostas.ano,
        propostas.titulo, propostas.edicao, propostas.diapub, propostas.mespub, propostas.anopub,
        propostas.coleccao, propostas.ISBN,  propostas.preco,
        propostas.editor, propostas.EndEditor,  propostas.urgencia);
        for(i=0; i<4 ;i++)
           fprintf(f,"%s;\n", propostas.autores[i]);
    fclose(f);
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.