Strtok Problem.

This is a discussion on Strtok Problem. within the C Programming forums, part of the General Programming Boards category; Code: void OrdemProducao (char *pOP){ //OP CD_FABRICA BATCH_ID CD_PESAGEM CD_BALANCA ITEM_DESC1 char *cValor = NULL; int iTab, iCont=1; char CD_FABRICA ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    158

    Strtok Problem.

    Code:
    void OrdemProducao (char *pOP){	//OP	CD_FABRICA	BATCH_ID	CD_PESAGEM	CD_BALANCA	ITEM_DESC1
    	
    	char *cValor = NULL;
    	int iTab, iCont=1;
    	
    	char CD_FABRICA [3], BATCH_ID [4], CD_PESAGEM [6], CD_BALANCA [3], ITEM_DESC1 [11];
    	char *Memoria;
    	
    	cValor = strtok(pOP, "	"); 	// Procura Pelo TAB
    	
    	while (cValor){
    			switch (iCont){
    				case 1:
    					strcpy (CD_FABRICA, cValor);
    				case 2:
    					strcpy (BATCH_ID,cValor);
    				case 3:
    					strcpy (CD_PESAGEM,cValor);
    				case 4:
    					 strcpy (CD_BALANCA,cValor);
    				case 5:
    					strcpy (ITEM_DESC1,cValor);
    				default:
    					break;
    			}
    			cValor = strtok(NULL, "	");
    			iCont++;
    	}
    	
    	printCom1 ("1: %s 2: %s 3: %s 4: %s 5: %s", CD_FABRICA, BATCH_ID, CD_PESAGEM, CD_BALANCA, ITEM_DESC1);
    }
    I need separate the code of string by char TAB

    I send F02 5256 4561 B01 TESTE DESC

    And Result is:

    Result: 1: 2: 5256 3: 4: B01 5: TESTE DESC

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And do you have a question?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    in The third case, I Got not expected NULL

    Code:
    case 3:
    	strcpy (CD_PESAGEM,cValor);

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	cValor = strtok(pOP, "	"); 	// Procura Pelo TAB
    perhaps you should have \t in the quotes - compiler may not accept control character.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by sergioms View Post
    I need separate the code of string by char TAB
    If you mean a tab as in multiple spaces you can use \t as a delimiter and it works:
    cValor = strtok(pOP, "\t");
    It matches two or more spaces in a row.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,338
    > strcpy (CD_FABRICA, cValor);
    1. Your string isn't long enough to store "F02" AND the \0 which follows it.
    2. Your cases need break; statements, otherwise you just keep doing the same thing to all strings (trashing previous results).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    i changed code,

    but continue same....

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So post the new code...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    158
    Code:
    void OrdemProducao (char *pOP){	//OP	CD_FABRICA	BATCH_ID	CD_PESAGEM	CD_BALANCA	ITEM_DESC1
    	
    	char *cValor = NULL;
    	int iTab, iCont=1;
    	
    	char CD_FABRICA [5], BATCH_ID [11], CD_PESAGEM [9], CD_BALANCA [5], ITEM_DESC1 [12];
    	
    	cValor = strtok(pOP, "\t"); 	// Procura Pelo TAB
    	
    	while (cValor){
    			switch (iCont){
    				case 1:
    					strcpy (CD_FABRICA, cValor);
    					break;
    				case 2:
    					strcpy (BATCH_ID,cValor);
    					break;
    				case 3:
    					strcpy (CD_PESAGEM,cValor);
    					break;
    				case 4:
    					 strcpy (CD_BALANCA,cValor);
    					 break;
    				case 5:
    					strcpy (ITEM_DESC1,cValor);
    					break;
    				default:
    					break;
    			}
    			cValor = strtok(NULL, "\t");
    			iCont++;
    	}
    	
    	printCom1 ("1: %s 2: %s 3: %s 4: %s 5: %s", CD_FABRICA, BATCH_ID, CD_PESAGEM, CD_BALANCA, ITEM_DESC1);
    }
    Ok, now It's Correct!

    Thanks for all again!
    I love you hehehe

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21