Jump to content

Recommended Posts

Posted

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.

Posted (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 by BahausMen

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.