How to compare string in text file with user imput and do validation?

Asked

Viewed 634 times

-1

At the end of this code, I’m trying to read the names of some countries and compare with user input, but I’m not getting results. It keeps repeating. If it finds, the program has to follow the flow normally.

void cadastrarDadosAvioes(Aviao *aviao, int *contaAviao, FILE *arquivo, int *baseInicial){
    int opcao=0, i, j, flag=1;
    char nomeAux[MAX], *result;
    strcpy(nomeAux, "");

    FILE *paises;

    //acrescenta dodos ao final ou faz leitura/escrita de arquivo
    arquivo = fopen("cadastroAvioes.txt", "ab");

    //arquivo dos paises

    paises = fopen("paises.txt", "r");

    if(arquivo=NULL){
        printf("\nNao foi possivel abrir o arquivo!\n");
        exit(EXIT_FAILURE);
    }else{    
        do{

            if(*contaAviao==10){
                *baseInicial = (*contaAviao)*2;
                aviao = (Aviao*)realloc(aviao, (*baseInicial)*sizeof(Aviao));
            }

            for(i=0;i<*baseInicial;i++){
                if(stricmp((aviao+i)->codigoIdentificacao, "")==0){
                    do{
                        strcpy((aviao+i)->codigoIdentificacao, leValidaCodigoAviao("DIGITE O CODIGO DO AVIAO:", "\nCODIGO DIGITADO INVALIDO!\n"));
                        for(j=0;j<*baseInicial;j++){
                            if(stricmp((aviao+i)->codigoIdentificacao, (aviao+j)->codigoIdentificacao)==0&&i!=j){
                                flag=0;
                                printf("\nCodigo ja existe!\n");
                            }else{
                                flag=1;
                            }
                        }

                    }while(!flag);

                    do{
                        strcpy((aviao+i)->modeloAviao, leValidaTexto("DIGITE O MODELO DO AVIAO:", "\nMODELO NAO PODE SER VAZIO!\n"));
                        for(j=0;j<*baseInicial;j++){
                            if(stricmp((aviao+i)->modeloAviao, (aviao+j)->modeloAviao)==0&&i!=j){
                                flag=0;
                                printf("\nEsse modelo já existe!\n");
                            }else{
                                flag=1;
                            }
                        }

                    }while(!flag);

                    leValidaCapacidade("INFORME A CAPACIDADE DO AVIAO:", "\nCAPACIDADE INVALIDA!\n", &(aviao+i)->capacidade);

                    do{
                        strcpy((aviao+i)->nomeCliente, leValidaTexto("DIGITE O NOME DO CLIENTE:", "\nNOME INVALIDO!\n"));
                        for(j=0;j<*baseInicial;j++){
                            if(stricmp((aviao+i)->nomeCliente, (aviao+j)->nomeCliente)==0&&i!=j){
                                flag=0;
                                printf("\nEsse nome já existe!\n");
                            }else{
                                flag=1;
                            }
                        }

                    }while(!flag);
                    /*
                    strcpy((aviao+i)->nomePais, leValidaTexto("DIGITE O NOME DO PAIS:", "\nPAIS INVALIDO!\n"));
                    paises = fopen("paises.txt", "r");
                    if(paises=NULL){
                        printf("\nArquivo não existe!\n");
                        exit(EXIT_FAILURE);
                    }else{
                        //Aqui estou tentando ler o arquio paises.txt

                        while(fscanf(arquivo, "%s", &nomeAux)){
                            printf("%s\n", nomeAux);
                        }
                    }
                    */

                    do{
                        strcpy((aviao+i)->nomePais, leValidaTexto("DIGITE O NOME DO PAIS:", "\nPAIS INVALIDO!\n"));
                        if(!paises){
                            printf("\nArquivo não existe!\n");
                            exit(EXIT_FAILURE);
                        }else{
                            while(fscanf(paises, "%s", &nomeAux)!=EOF){
                                if(strcmp((aviao+i)->nomePais, nomeAux)==0){
                                    flag=1;
                                    break;
                                }else{
                                    flag=0;
                                    break;
                                }
                            }
                        }

                    }while(!flag);

                    break;
                }
            }

            //atualiza contador
            *contaAviao+=1;

            printf("(1)- CADASTRAR OUTRO AVIAO\n");
            printf("(2)- RETORNAR AO MENU\n\n");
            scanf("%d", &opcao);

            if(opcao == 2){
                getch();
                return;
            }

        }while(opcao==1&&(*contaAviao<*baseInicial));
        fclose(arquivo);
    }   
}
  • Here: fscanf(countries, "%s", &nameAux) does not have this & because nameAux is already an address. The string start address.

1 answer

1


I believe you’re referring to this here:

while(fscanf(paises, "%s", &nomeAux)!=EOF){
    if(strcmp((aviao+i)->nomePais, nomeAux)==0){
        flag=1;
        break;
    }else{
        flag=0;
        break;
    }
}

You have not described the file format. But I believe it is a country name per line. In that case better use the fgets() who reads line by line:

while (fgets(nomeAux, MAX, paises)) {

See that the fgets() may include the end-of-line character (\n) in the string returned. You will have to search for the character and remove.

To fscanf() reads to the first white space, then a line with Estados Unidos returns twice: once to Estados and another to Unidos.

  • Thank you, man! It’s a text file. I was having difficulty with fgets, so I ended up going to fscanf. And yes, it’s a country name per line. My problem is that I want to use the comparison result as a flag. As you may have noticed, my program is all based on this.

  • Question: Let’s suppose that I am populating a struct vector and I want to write to each thing registered in the file (the data already validated in the vector). It makes sense what I’m trying to do or there’s an easier and better way?

  • I’m not sure I understand exactly what you want. I suggest posting a new question and include more details.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.