Problems with C files

Asked

Viewed 61 times

-1

Hey there, guys! I’m making a code for the records of patients who tested positive for COVID. When the patient is registered, their data is sent to a txt file. The problem occurs when a patient is over 65 years old and must have their data recorded in another file separate from other patients. At the moment of checking the age and recording the data in another file, I have problems at the time of recording. I can’t get it recorded in the file I want. I’m a beginner in C and I’d like your help.

From the use of if it is possible to create another file to put the data of the patient who is over 65 years?

void inputPaciente(){
    PACIENTE pct;
    FILE* arquivo;
    
    arquivo = fopen("lista_pacientes.txt", "ab");
    if(arquivo == NULL){
        printf("Problemas na abertura do arquivo.");
    }
    else{
        do{   
              cabecalho();
              
              printf("\nINSIRA OS DADOS DO PACIENTE\n");
              
              fflush(stdin);
              printf("\nNome: ");
              scanf("%[^\n]s", pct.nome);
              
              fflush(stdin);
              printf("\nCPF: ");
              scanf("%[^\n]s", pct.CPF);
              
              fflush(stdin);
              printf("\nTelefone: ");
              scanf("%[^\n]s", pct.telefone);

              fflush(stdin);
              printf("\nEndereço completo: ");
              scanf("%[^\n]s", pct.endereco);
              
              fflush(stdin);
              printf("\nCEP: ");
              scanf("%[^\n]s", pct.CEP);
              
              fflush(stdin);
              printf("\nData De nascimento (DD/MM/AAAA): ");
              scanf("%d %d %d", &pct.aniv.dia, &pct.aniv.mes, &pct.aniv.ano);
              
              fflush(stdin);
              printf("\nDigite o email do paciente: ");
              scanf("%[^\n]s", pct.email);
              
              fflush(stdin);
              printf("\nData do diagnostico (DD/MM/AAAA): ");
              scanf("%s", pct.dataDoDiagnostico);
              
              fflush(stdin);
              printf("\nO paciente possui alguma comorbidade?: ");
              scanf("%[^\n]s", pct.comorbidade);
              
              fwrite(&pct, sizeof(PACIENTE), 1, arquivo);
              
              printf("\nDeseja continuar (s/n)?");
              
        }while(getche() == 's');
        fclose(arquivo);
    }
}
  • If you want to write to another file then you need to have another file (declare, open, write and close), your routine works with a single file.

  • The problem is I don’t know how and at what point in the code I should do this.

  • Where you have a reference to arquivo do the same thing with, for example, arquivo65. Treat your dataDoDiagnostico not as a string but as composed of the day, month and year fields. Before recording check if the date of birth is <= date of diagnosis - 65 years and record either on file or in archive65.

  • I don’t think you should record it in another file, it should be a property of the "Patient" object. And I can’t understand what’s in your doubt...

  • Now I understand what must be done. Thank you, people!!!

1 answer

0


Whenever you write anything inside the file you have to use the fflush function and put the file as parameter, otherwise it will not save. Example:

scanf("%[^\n]s", pct.comorbidade);
          
          fwrite(&pct, sizeof(PACIENTE), 1, arquivo);
          fflush(arquivo);
          printf("\nDeseja continuar (s/n)?");

See this reference: http://www.cmaismais.com.br/referencia/cstdio/fflush/

  • 1

    The user with a point of reputation owns the publishing privilege. Featured in the description of privilege: The most basic privilege of all, the right to ask a question and the right to contribute an answer. This is usually available to anyone regardless of reputation level..

  • 1

    Do not use the main website to make operating complaints, for this there is the META. The debates opened in META should be about the operation of the site, community questions, bugs, proposed improvements and new features. Read: What is "META"? How it works?

Browser other questions tagged

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