Problem with reading file C language

Asked

Viewed 61 times

0

Good evening guys. So I’m doing a two-step assembler, so I have to upload a file in the format. a and assemble a file in the format . mif, only when I make the first step to save the addresses of the "subroutines" the file opens, copies the Abels, addresses according to the architecture and the file passed, but when it arrives at the end of the file it does not leave the while and in the terminal appears to me the following error: * stack Smashing Detected *: terminated. The strange thing is that I tested with a file . txt and this error does not happen. If anyone can help I thank you. Follow the code below:

  #include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Endereco{
  char endereco[100];
  char binario[10];
} Endereco;

//Subrotinas
void PrimeiroPasso(char arquivo[], Endereco enderecos[]);
void ConvertDecimalToBinario(int num, char endereco[10]);
void ConvertDecimalToHexadecimal(int num, char endereco[2]);

//Função Principal
int main(int argc, char const *argv[]) {
  FILE *pFile;
  char arquivo[100];
  Endereco enderecos[64];
  printf("%ld\n", sizeof(enderecos)/sizeof(enderecos[0]));
  printf("Informe o arquivo que desejar traduzir: ");
  scanf(" %[^\n]s", arquivo);
  while(1){
    pFile = fopen(arquivo, "r");
    if(pFile != NULL){
      fclose(pFile);
      break;
    }
    printf("Arquivo invalido desejar informa outro arquivo? Digite s para sim e n para sair da aplicação");
    while(1){
      char confirmaSair;
      scanf(" %c", &confirmaSair);
      if((confirmaSair == 'n') || (confirmaSair == 'N')){
        return 0;
      }
      else if((confirmaSair == 's') || (confirmaSair == 'S')){
        break;
      }
      else{
        printf("Opcao invalida\n");
      }
      printf("desejar informa outro arquivo? Digite s para sim e n para sair da aplicação\n");
    }
    printf("Informe o arquivo que desejar traduzir: ");
    scanf(" %[^\n]s", arquivo);
  }
  PrimeiroPasso(arquivo, enderecos);
  //Para teste
  printf("%s: %s\n", enderecos[0].endereco, enderecos[0].binario);
  printf("%s: %s\n", enderecos[1].endereco, enderecos[1].binario);
  printf("%s: %s\n", enderecos[2].endereco, enderecos[2].binario);
  return 0;
}


//Subrotina para executar o primeiro passo do montador
void PrimeiroPasso(char arquivo[], Endereco enderecos[]){
  FILE *pFile = fopen(arquivo, "r+");
  int linhaDoc = 0;
  int ctrlVetorStruct = 0;
  while(!feof(pFile)){
    char bufferAux = fgetc(pFile);
    printf("%c\n", bufferAux);
    if(bufferAux == '_'){
      int n = 1;
      char enderecoAux2[100];
      enderecoAux2[0] = '_';
      while(1){
        char buffer;
        buffer = fgetc(pFile);
        if(buffer == ':'){
          char enderecoAux[10];
          int aux = linhaDoc*2;
          enderecoAux2[n] = '\0';
          strcpy(enderecos[ctrlVetorStruct].endereco, enderecoAux2);
          ConvertDecimalToBinario(aux,enderecoAux);
          strcpy(enderecos[ctrlVetorStruct].binario, enderecoAux);
          printf("%s: %s\n", enderecos[ctrlVetorStruct].endereco, enderecos[ctrlVetorStruct].binario);
          ctrlVetorStruct++;
          break;
        }
        else if(buffer == '\n'){
          linhaDoc++;
          break;
        }
        else{
          enderecoAux2[n] = buffer;
          n++;
        }
      }
    }
      else if(bufferAux == '\n'){
        linhaDoc++;
      }
  }
  fclose(pFile);
  return;
}

//Função de conversão de decimal para binário, necessária para converter e construir um os instruções
void ConvertDecimalToBinario(int num, char endereco[10]){
  int n = 8;
  int resto;
  while(1){
    endereco[n] = '0' + num%2;
    num = num/2;
    n--;
    if((num == 1) || (num == 0)){
      endereco[n] = '0'+ num;
      n--;
      if(n >= 0){
        int i = 0;
        while(i <= n){
          endereco[i] = '0';
          i++;
        }
      }
      endereco[9] = '\0';
      return;
    }
  }
}
  • put the test data, otherwise it becomes difficult...

1 answer

0

The end of file test is in wrong place...
You have to test right after doing a reading operation:

// while(!feof(pFile)) // <---------------  ERRADO
while (1)
{
  // char bufferAux = fgetc(pFile); // <----------- ERRADO
  int bufferAux = fgetc(pFile);
  if (bufferAux == EOF)
  {
    // fim de arquivo
    // ...
    break;
  }

  printf("%c\n", bufferAux);

  // ...

Also, the data entry part of the file name would probably look better in a separate function.

  • I remodeled it, put it in while, but that’s not what was doing the error occurs is that I had to put the Aux2 address array as a dynamic array, in certain lines there were strings larger than 200. But thank you so much for helping [ ]s.

Browser other questions tagged

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