How to find something specific inside a Text file

Asked

Viewed 89 times

1

Hello, I have to check the grades of a certain student and if his average is greater than 5 show the name so I have no idea how to do it... nor to ask how does it so excuse me if I was unclear

struct ALUNOS{
    char nome[50];
    float nota1, nota2;
}aluno;

void cadastra(){
    FILE *arquivo;
    ALUNOS aluno;
    char resp, numstr[40];

    arquivo = fopen("alunoscad.txt","wt");
    if (arquivo == NULL){
        printf("\n Erro abertura!");
        exit(1);
    }
    do{
        printf("\n Nome: ");
        gets(aluno.nome);
        printf("\n Nota 1: ");
        gets(numstr);
        aluno.nota1 = atof(numstr);
        printf("\n Nota 2: ");
        gets(numstr);
        aluno.nota2 = atof(numstr);



            fprintf(arquivo,"%s",aluno.nome);
            fprintf(arquivo,"\n%.2f",aluno.nota1);
            fprintf(arquivo,"\n%.2f\n",aluno.nota2);


        printf("\n Deseja cadastrar mais? \n");
                do {
            resp = toupper(getch());
        } while (resp != 'S' && resp != 'N');

    }while(resp=='S');


    fclose(arquivo);


}

void mostra (){
    FILE *arquivo;
    ALUNOS aluno;

    char ch;
    int achou = 0;
    if ((arquivo = fopen("alunoscad.txt", "rt")) == NULL){
        printf("\n Erro abertura");
        exit(1);
    }
    while ((ch = getc(arquivo)) !=EOF){


        fgets()
    }
        fclose (arquivo);

}

int main(){
    cadastra();
    mostra();
}

this is all my code

  • Ola Giovanni Dias, I suggest you take the tour to learn how to ask: http://answall.com/tour. A way to search in text files and using regular expressions, or you can mark up your file to make it easier to search.

1 answer

0

consequence something more or less like this but it does not read the file and without the struct

void mostra (){
FILE *arquivo;
ALUNOS aluno[100];
int aux;
char ch;
int achou = 0;
if ((arquivo = fopen("alunoscad.txt", "rt")) == NULL){
    printf("\n Erro abertura");
    exit(1);
}
for(int i=0;i<b;i++){

    aux = 0;
    aux = (aluno[i].nota1+aluno[i].nota2)/2;
    printf("Media: \n%d",aux);
    if(aux>=5){

        printf("Aluno: \n%s",aluno[i].nome);
            }
    }
}

Browser other questions tagged

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