-1
I have to read a file in which the first line represents the number of registered students and in the following lines occurs the name of the student followed by his grade in the two tests. It seems to me that the problem is in reading the file, but I have tried several ways to remedy the problem but it persists. File example:
3
ZE CARLOS
8.5
10.0
ANTONIO SANTOS
7.5
8.5
SEBASTIAO OLIVEIRA
5.0
6.0
int main()
{
FILE *pFile;
char nomearq[25];
float *medias, temp1=0, temp2=0;
int qntalunos, i, j;
printf("Entre com o nome do arquivo no qual as informacoes estao armazenadas: ");
fflush(stdin);
gets(nomearq);
//Abertura do arquivo em modo de leitura
pFile=fopen(nomearq, "r");
//Verificacao
if(pFile==NULL)
{
printf("\nErro em abrir o arquivo\n\n");
exit(1);
}
else
{
printf("\nArquivo aberto com sucesso\n\n");
}
//Adquirindo a quantidade de alunos presente no arquivo
fscanf(pFile,"%d", &qntalunos);
char nomes[qntalunos][50];
//Alocando dinamicante
medias=(float *)malloc(qntalunos*sizeof(float));
//Verificando
if(medias==NULL)
{
printf("\nErro na alocacao dinamica do vetor de medias\n\n");
exit(1);
}
//Lendo o arquivo
for(i=0; i<qntalunos; i++)
{
fgets(nomes[i],50, pFile);
fscanf(pFile,"%f", &temp1);
fscanf(pFile,"%f", &temp2);
medias[i]=(temp1+temp2)/2.0;
}
printf("Alunos abaixo da media:\n\n");
for(i=0; i<qntalunos; i++)
{
if(medias[i]<7.0)
{
printf("Nome: %s\n", nomes[i]);
printf("Media: %.2f\n", medias[i]);
}
}
fclose(pFile);
free(medias);
return 0;
}
What exactly is the error of the programme? when implementing what the programme does wrong?
– IanMoone
None of the data is being stored correctly, I tried to print them later, for example the medias array, and there is nothing or some crazy numbers appear. Same with the name array
– Marco Antonio Schneider