BahausMen Posted June 2, 2014 at 09:04 PM Report #557981 Posted June 2, 2014 at 09:04 PM Boas pessoal, eu tenho o seguinte programa em baixo e tenho como objectivo conseguir dividir uma string. instr = "i:100.BRAGA:BRAGA:S.VITOR" instr: i:100.BRAGA:BRAGA:S.VITOR Eu estive aqui SIM ENTROczxczxc! aux0: BRAGA aux1: BRAGA: Cenas: 0 BRAGA: aux0: (null) Falha de segmentação (core dumped) o objetivo era ser: instr: i:100.BRAGA:BRAGA:S.VITOR Eu estive aqui SIM ENTROczxczxc! aux0: BRAGA aux1: BRAGA: Cenas: 0 BRAGA: aux0: BRAGA aux1: BRAGA: Cenas: 1 BRAGA: aux0: S.VITOR aux1: S.VITOR: Cenas: 2 S.VITOR: o código da main é o seguinte: int main(int argc, const char * argv[]) { int fd,i=0; char *ninc; mkfifo("MYPIPE",0666); char instr[bUFFER],*tipo,*path,*aux=NULL; char *loc[NIVEIS]; if ((fd = open("MYPIPE", 0666)) < 0) perror("Sem comunicacao ... "); else { for(i=0;i<=NIVEIS+2;i++) { loc[i]=malloc(sizeof(char)*BUFFER); } while(read(fd,instr,BUFFER)!=0) { printf("instr: %s\n",instr); tipo= strtok(instr,":"); ninc=strtok(NULL,"."); printf("Eu estive aqui\n"); if( tipo[0] == 'i') { printf("SIM ENTROczxczxc!\n"); aux=strtok(NULL,":"); printf("aux0: %s\n", aux); strcat(aux,":\0"); printf("aux1: %s\n", aux); strcpy(loc[0],aux); printf("Cenas: %d %s\n",0,loc[0]); for(i=1;aux && i<=NIVEIS;i++) { aux=strtok(NULL,":"); printf("aux0: %s\n", aux); strcat(aux,":\0"); printf("aux1: %s\n", aux); strcpy(loc[i],aux); printf("Cenas: %d %s\n",i,loc[i]); } printf("ola\n"); if(!fork()) { incrementar1(loc,atoi(ninc)); _exit(0); } } if(tipo[0] == 'a') { path=strtok(NULL,"-"); for(aux=strtok(NULL,":");aux && i<NIVEIS;aux=strtok(NULL,":")) { strcat(aux,":"); strcpy(loc[i++],aux); } if(!fork()) { agregar1(loc,atoi(ninc),path); _exit(0); } } } // incrementar(nome,valor); //agregar(nome,1,"./Resposta_Agregar/"); } return 0; } O problema nao é o Segmention Fault ... isso percebemos porque estoura. Queria saber era porque é que ele perde a string, no strtok .. antes de chegar ao segundo BRAGA.
HappyHippyHippo Posted June 2, 2014 at 09:12 PM Report #557985 Posted June 2, 2014 at 09:12 PM a função strtok tem como efeito secundário a destruição da string, logo deverá ser chamada sobre uma cópia da string original. http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
BahausMen Posted June 2, 2014 at 09:21 PM Author Report #557991 Posted June 2, 2014 at 09:21 PM (edited) a função strtok tem como efeito secundário a destruição da string, logo deverá ser chamada sobre uma cópia da string original. http://pubs.opengroup.org/onlinepubs/009695399/functions/strtok.html mas ele destroi o que já li correto? mudei para uma cópia strcpy(instr2,instr) e passei a usar a cópia e mesmo assim fica igual. Edited June 2, 2014 at 09:21 PM by BahausMen
lusitan Posted June 2, 2014 at 09:30 PM Report #557993 Posted June 2, 2014 at 09:30 PM Isso não compila! Qual o valor de BUFFER e NIVEIS?
BahausMen Posted June 2, 2014 at 09:34 PM Author Report #557995 Posted June 2, 2014 at 09:34 PM (edited) BUFFER = 1024 NIVEIS =2 Não compila porque não tem o codigo todo, mas o problema após vários prints prende-se com o strtok. Edited June 2, 2014 at 10:16 PM by BahausMen
HappyHippyHippo Posted June 2, 2014 at 10:14 PM Report #558003 Posted June 2, 2014 at 10:14 PM talvez queiras reformular a definição de NIVEIS ... de certeza que é um array com os valores 0, 1 e 2 ?? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
BahausMen Posted June 2, 2014 at 10:16 PM Author Report #558004 Posted June 2, 2014 at 10:16 PM int niveis = 2; foi lapso meu a explicar.
HappyHippyHippo Posted June 2, 2014 at 10:28 PM Report #558008 Posted June 2, 2014 at 10:28 PM sabes porque é que o processo é destrutivo ? se sim, sabes que o que estás a fazer no código seguinte estraga tudo: strcat(aux,":\0"); IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
BahausMen Posted June 2, 2014 at 10:33 PM Author Report #558009 Posted June 2, 2014 at 10:33 PM guardo o aux no loc. depois o aux é substituido pelo novo retorno do strtok. Existe alguma coisa que esteja a escapar ?
HappyHippyHippo Posted June 2, 2014 at 10:37 PM Report #558010 Posted June 2, 2014 at 10:37 PM respondes com três linhas de texto onde nenhuma delas refere o código que apresentei no post anterior ... IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
BahausMen Posted June 3, 2014 at 03:18 PM Author Report #558079 Posted June 3, 2014 at 03:18 PM Conseguimos resolver o problema 🙂 Obrigado.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now