fscanf在没有整数存在的情况下不会进入下一行

时间:2018-05-29 18:17:22

标签: c

我有以下文本文件:

123456789 0 Paulo Silva

555666777 2 Marta Nunes Cabral
4   1   12  12  2017    17  12  2017
5   0   20  04  2018

123123123 3 Maria Pimentel
5   2   01  10  2017    10  10  2017
5   1   13  01  2018    15  01  2018
4   0   17  04  2018

我已完成所有代码,但每当它尝试从此部分转到下一行

5   0   20  04  2018  //27  04  2018

它跳出了阅读循环,但是如果我把一些像评论中的那些值那么它就会读得很好。

这是它读取文件的代码:

clientes * criaClientes(pClientes cli, int *tam){
FILE *f;
int nif = 0;
int nAlug = 0;
char nome[25] = "";
int idGuit = 0;
int estado = 0;
int count = 0;
int diaI = 0, diaE = 0, mesI = 0, mesE = 0, anoI = 0, anoE = 0;

pClientes novo;
pAlugueres novoAl;

f = fopen("Clientes.txt", "rt");
if(f == NULL)
    printf("Erro ao ler o ficheiro de texto");

while(fscanf(f, " %d %d", &nif, &nAlug) == 2){

    printf("Contagem: %d\n", count + 1);

    novo = malloc(sizeof(clientes));
    novo->al = NULL;

    if(novo == NULL){
        printf("Erro ao alocar memoria!");
        return cli;
    }

    fscanf(f, "%[^\n]", nome);
    removeSpaces(nome);

    novo->NIF = nif;
    novo->nAlugueres = nAlug;
    strcpy(novo->nome, nome);
    novo->banido = 0;

    while(nAlug != 0){
        fscanf(f, " %d %d %d %d %d", &idGuit, &estado, &diaI, &mesI, &anoI);
        nAlug--;

        novoAl = malloc(sizeof(alugueres));

        if(novoAl == NULL){
            printf("Erro ao alocar memoria!");
            return cli;
        }

        novoAl->ID = idGuit;
        novoAl->estado = estado;
        novoAl->dataInicio.dia = diaI;
        novoAl->dataInicio.mes = mesI;
        novoAl->dataInicio.ano = anoI;

        if(fscanf(f, " %d %d %d\n", &diaE, &mesE, &anoE) == 3){
            novoAl->dataEntrega.dia = diaE;
            novoAl->dataEntrega.mes = mesE;
            novoAl->dataEntrega.ano = anoE;
        }
        else{
            novoAl->dataEntrega.dia = 0; //Can this be the problem?
            novoAl->dataEntrega.mes = 0;
            novoAl->dataEntrega.ano = 0;
        }

        novoAl->prox = novo->al;
        novo->al = novoAl;
    }

    //Adicionar no inicio da lista ligada
    novo->prox = cli;
    cli = novo;

    count++;

}
    *tam = count;


    fclose(f);

    return cli;

}

如果部分找不到值,那么将它置于0可以解决问题吗?

编辑:经过大量的反复试验后,我得到了这个解决方案(只是粘贴了部分代码而不是将问题集中在一起)

        while(nAlug != 0){
        fscanf(f, " %d %d %d %d %d", &idGuit, &estado, &diaI, &mesI, &anoI);

        nAlug--;

        novoAl = malloc(sizeof(alugueres));

        if(novoAl == NULL){
            printf("Erro ao alocar memoria!");
            return cli;
        }

        novoAl->ID = idGuit;
        novoAl->estado = estado;
        novoAl->dataInicio.dia = diaI;
        novoAl->dataInicio.mes = mesI;
        novoAl->dataInicio.ano = anoI;

        if(fgets(buffer, sizeof(buffer), f) != NULL){

            int itens = sscanf(buffer, " %d %d %d", &diaE, &mesE, &anoE);
            printf("NUMERO DE ITENS: %d\n", itens);

            if(itens == 3){
                novoAl->dataEntrega.dia = diaE;
                novoAl->dataEntrega.mes = mesE;
                novoAl->dataEntrega.ano = anoE;
                itens = 0;
            }else{
                novoAl->dataEntrega.dia = 0;
                novoAl->dataEntrega.mes = 0;
                novoAl->dataEntrega.ano = 0;
            }
        }
        else{
            novoAl->dataEntrega.dia = 0;
            novoAl->dataEntrega.mes = 0;
            novoAl->dataEntrega.ano = 0;
        }

    novoAl->prox = novo->al;
    novo->al = novoAl;
}

0 个答案:

没有答案