Write to file . txt with C

Asked

Viewed 23 times

0

Hi guys!! Here I am again having problems with files... I made a program to register patients with covid. The data of these patients should be recorded in a txt file. The problem is this: the data is passed to txt, but they are all crowded into a single line. How I make each one to be recorded in different lines?

void cabecalho(){
    system("cls");
    printf("--------------------------------------\n");
    printf("          CADASTRO COVID-19           \n");
    printf("--------------------------------------\n\n");
}

void inputPaciente(){
      char nome[50];
      char endereco[50];
      char email[20];
      char CPF[12];
      char telefone[15];
      char CEP[10];
      char comorbidade[100];

    FILE* arquivo;
    
    arquivo = fopen("teste.txt", "ab");

    if(arquivo == NULL){
        printf("Problemas");
    }
    else{
        
        do{
            cabecalho();

            printf("\nINSIRA OS DADOS DO PACIENTE\n\n");
            
            fflush(stdin);
            printf("Nome: ");
            gets(nome);
            fprintf(arquivo, "\nNome: %s", nome);
            
            fflush(stdin);
            printf("\nCPF: ");
            gets(CPF);
            fprintf(arquivo, "\nCPF: %s", CPF);
            
            fflush(stdin);
            printf("\nTelefone: ");
            gets(telefone);
            fprintf(arquivo, "\nTelefone: %s", telefone);
            
            fflush(stdin);
            printf("\nEndereço completo: ");
            gets(endereco);
            fprintf(arquivo, "\nEndereço: %s", endereco);
            
            fflush(stdin);
            printf("\nCEP: ");
            gets(CEP);
            fprintf(arquivo, "\nCEP: %s", CEP);
            
            fflush(stdin);
            printf("\nDigite o email do paciente: ");
            gets(email);
            fprintf(arquivo, "\nEmail: %s", email);
            
            fflush(stdin);
            printf("\nO paciente possui alguma comorbidade?: ");
            gets(comorbidade);
            fprintf(arquivo, "\nComorbidades: %s", comorbidade);

            printf("\n\nDeseja continuar (s/n)?");

        }while(getche() == 's'); //repete o ciclo de cadastro
        fclose(arquivo); // fecha o arquivo
    }
}
  • I don’t know how the data can "get crowded into a single line" if the first thing you put in the format specification of the fprintf function was a ' n'. Another point to consider is replacing the function gets, whose use is not recommended by the function fgets.

No answers

Browser other questions tagged

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