How to omit space to enter only numbers in different lists

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct NBIG{
    int numero;
    struct NBIG *nseg;
    struct NBIG *nant;
}Nbig;    
int main(int argc,char *argv[])
{
    char *nf,ch;
    Nbig *vem=NULL,*resmulti=NULL,*dasoma=NULL;
    Nbig *lista=NULL;
    Nbig *lista2=NULL;
    int cont,x,y;
    FILE *arq;

    if (argc>3) {
        // ficheiro
        for(cont=3; cont < argc; cont++){

            nf=strcat(argv[cont], ".txt");
            printf("%s",nf);
            arq = fopen(nf, "r");
            if(arq == NULL)
                printf("Erro, nao foi possivel abrir o arquivo\n");
            else{
                fseek(arq, -1, SEEK_END);
                long posicao = ftell(arq);
                while(posicao >= 0){
                fseek(arq, posicao, SEEK_SET);
                fread(&ch, sizeof(char), 1, arq);
                if (ch == ' ') {
                    dasoma=soma(dasoma, lista);
                    lista=apaga(lista);
                    vem=apaga(vem);
                    posicao=posicao-1;
                }
                else{
                    vem=makenode();
                    vem->numero= atoi(&ch);
                    lista=insertfirst(lista,vem);
                    posicao--;
                }

            }
            printf("\n");
            mostra(dasoma);
            printf("\n");
            fclose(arq);

                }
            }

            //printf("%d Parametro: %s\n", cont,argv[cont]);
        }
    }
    else{
    ...
    }

In the file: 123 321

I do not understand why the code does not work. receives a file name as the 3rd argument and returns it.

  

agrv (14077,0x7fff8f271380) malloc: * error for object   0x7fd9f14026c0: pointer being freed was not allocated   * set a breakpoint in malloc_error_break to debug Abort trap: 6

The only function that uses malloc is

Nbig *makenode(){
    Nbig *R = (Nbig*)malloc(sizeof(Nbig));
    R->numero=0;
    R->nant=NULL;
    R->nseg=NULL;
    return(R);
}

Nbig *apaga(Nbig *L){
while (L!=NULL) {
    L=L->nseg;
    free(L);
}
return L;
}

Nbig *insertfirst(Nbig *A,Nbig *nv){
if(A==NULL){
    nv->nseg=NULL;
    nv->nant=NULL;
    return nv;
}
nv->nseg=A;
A->nant=nv;
return nv;
}
    
asked by anonymous 05.04.2018 / 10:29

1 answer

0

The problem is with your apaga function.

In it you always delete the next of the non-null element (which can be null). Try changing the test to something like this:

Nbig *apaga(Nbig *L) {
  if (L == NULL)
      return NULL;
  while (L->nseg!=NULL) {
    L=L->nseg;
    free(L);
  }
  free(L);
  return NULL;
}
    
06.04.2018 / 14:49