Разрушение стека при использовании Fgets - PullRequest
0 голосов
/ 21 октября 2019

Я пытаюсь получить данные из двоичного файла на картинке и правильно их прочитать. Однако я поступаю неправильно. Как я могу исправить свой код? Правильно ли использовать fgets в этом случае? Какова правильная реализация для этого случая?

PS: я опустил большую часть кода из-за правил сайта.

/* */

tipoErros
ObterMediaAluno(char *nomeArquivoNotas, char *dreAluno, float *media)
{

  /* */

  FILE *arquivoNotas;
  char buffer[COMPRIMENTO_DRE /* 9 */ + 1];
  unsigned numeroProvas;
  float notas[6];
  char *validacao;





  /* */

  if (!nomeArquivoNotas)
    return argumentoNulo;  /* 1 */





  /* */

  if (*nomeArquivoNotas == EOS)
    return argumentoVazio;  /* 2 */





  /* */

  arquivoNotas = fopen(nomeArquivoNotas, "rb");





  /* */

  if (!arquivoNotas)
  {
    fclose(arquivoNotas);
    return erroAbrindoArquivoBinario;  /* 4 */
  } /* if */





  /* */

  numeroProvas = (unsigned) (fgetc(arquivoNotas) - '0');




  while (fgets(buffer, (COMPRIMENTO_DRE /* 9 */ + 1), arquivoNotas) != NULL)
  {

    printf("\n\n%s\n\n", buffer);

    if (strcmp(buffer,dreAluno) == BEM_SUCEDIDO)
    {
      fgets(buffer, 5, arquivoNotas);
      notas[0] = strtof(buffer, &validacao);
      fgets(buffer, 5, arquivoNotas);
      notas[1] = strtof(buffer, &validacao);
      fgets(buffer, 5, arquivoNotas);
      notas[2] = strtof(buffer, &validacao);
      fgets(buffer, 5, arquivoNotas);
      notas[3] = strtof(buffer, &validacao);
      fgets(buffer, 5, arquivoNotas);
      notas[4] = strtof(buffer, &validacao);
      fgets(buffer, 5, arquivoNotas);
      notas[5] = strtof(buffer, &validacao);
      printf("\n\n%.2f %.2f %.2f %.2f %.2f %.2f\n\n\n", notas[0], notas[1], notas[2], notas[3], notas[4], notas[5]);
    } /* if */
    else
    {
      fgets(buffer, 25, arquivoNotas);
      printf("\n\n%s\n\n", buffer);
    } /* else */  

  } /* while */


  return ok;  /* 0 */
} /* ObterMediaAluno */

enter image description here

/* output */



UBUNTU 08 --> ./testeMedia notas_binario 111239451


8.90 6.90 7.40 4.90 0.00 1.00




07.309.703.300.001.500.0



04.400.001.402.800.001.5





*** stack smashing detected ***: <unknown> terminated
Abortado (imagem do núcleo gravada)
UBUNTU 08 -->
...