Question about strtok

This is a discussion on Question about strtok within the C Programming forums, part of the General Programming Boards category; Hello! I'm trying to read from files some lines? I also use the strtok method to get words from each ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    N?A
    Posts
    23

    Question about strtok

    Hello!
    I'm trying to read from files some lines?
    I also use the strtok method to get words from each line!
    I need to give warning if there is too many parameters(word) or too little
    How i can do that ?
    Without strtok? maybe some kind of command or something like that?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    strtok() is certainly usable for this purpose. There are obviously other methods too [like writing your own parser function]. Without knowing more about what the input is supposed to look like it is impossible to answer "what is the best way".

    --
    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
    Mar 2008
    Posts
    10
    If you are trying to count the number of words in each line try this:
    1. read in each line into a buffer using fgets
    2. use strtok on the buffer using a while loop and have a counter in the loop

  4. #4
    Registered User
    Join Date
    Feb 2008
    Location
    N?A
    Posts
    23
    Quote Originally Posted by matsp View Post
    strtok() is certainly usable for this purpose. There are obviously other methods too [like writing your own parser function]. Without knowing more about what the input is supposed to look like it is impossible to answer "what is the best way".

    --
    Mats
    I use the strtok Not for counting the words
    I use it because i need to take words from the line
    But if the number of words in line don't feet
    I need to show some warning!
    so my question is : if there is a command that tell me the numbers of words in line

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    if there is a command that tell me the numbers of words in line
    no - count them by yourself
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not that I can immediately think of. But if you are parsing the line anyways to find the words in it, then you may just as well try to get as many as you expect to get and if that either fails [too few words] or you got something left over [too many words] print a relevant error message.

    --
    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.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Location
    N?A
    Posts
    23
    Quote Originally Posted by vart View Post
    no - count them by yourself
    Thank's
    another question:
    There is problems with the strcmp somtimes it's worke and sometimes Not and it's not only in my program also in my friends program
    so someytimes i use the strncmp command and now it's work?
    why it's happening?

    One more question?
    When I send 2 Dimensions array to a function
    in the Debug mode it's show me that the function get 1 dimension array
    But actually the function get 2 dimensions?
    why it's happening(I use Visual Studio 2005)

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by c9dw2rm8 View Post
    Thank's
    another question:
    There is problems with the strcmp somtimes it's worke and sometimes Not and it's not only in my program also in my friends program
    so someytimes i use the strncmp command and now it's work?
    why it's happening?

    One more question?
    When I send 2 Dimensions array to a function
    in the Debug mode it's show me that the function get 1 dimension array
    But actually the function get 2 dimensions?
    why it's happening(I use Visual Studio 2005)
    strcmp always works; I would guess sometimes you use it correctly and sometimes you don't. It returns a negative value if the first argument comes first (ASCIIbetically), 0 if they agree, and a positive number if the second argument comes first.

    I'm not very familiar with the VS debugger, so I'll hand that off.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Location
    N?A
    Posts
    23
    Quote Originally Posted by tabstop View Post
    strcmp always works; I would guess sometimes you use it correctly and sometimes you don't. It returns a negative value if the first argument comes first (ASCIIbetically), 0 if they agree, and a positive number if the second argument comes first.

    I'm not very familiar with the VS debugger, so I'll hand that off.
    in My program i need somtimes to find the word CHECK
    and sometime CHECKMATE
    maybe because both of them begin with the same sub string CHECK it's make problems?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by c9dw2rm8 View Post
    in My program i need somtimes to find the word CHECK
    and sometime CHECKMATE
    maybe because both of them begin with the same sub string CHECK it's make problems?
    Only if you don't compare correctly.

    --
    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.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Location
    N?A
    Posts
    23
    Quote Originally Posted by matsp View Post
    Only if you don't compare correctly.

    --
    Mats

    Code:
    while (fgets(line,LINE_LENGTH,input)!=NULL){
    	command=strtok(line," \t\n\r");
    	if (command==NULL) continue; //empty line
    	if (strcmp(command,"NEW")==0){
    		tool=strtok(NULL," \t\n\r");
    		to=strtok(NULL," \t\n\r");
    		color=strtok(NULL," \t\n\r");
    		if (color==NULL || tool==NULL || to==NULL){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue;
    		}
    		AddPiece(color,tool,to,output);
    
    	}//end NEW
    
    	else if (strcmp(command,"MOVE")==0){
    		from=strtok(NULL," \t\n\r");//from
    		to=strtok(NULL," \t\n\r");//to
    		if ((from==NULL)||(to==NULL)){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue;
    		}
    		MoveTool(from,to,output);
    	}//end MOVE
    
    	else if(strcmp(command,"CHECKMATE")==0){
    		color=strtok(NULL," \t\n\r");
    		if (color==NULL){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue; //end the current session of the loop
    		}
    		CheckCheckMate(color,dest);
    		
    	}//end CHECKMATE
    
    
    	else if(strcmp(command,"CHECK")==0){
    		color=strtok(NULL," \t\n\r");
    		Result=CheckCheck(color,output);
    		if (color=NULL){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue;
    		}
    		if (Result==0)//Not good Color
    			printMessage(output,NO_SUCH_COLOR);
    	
    		if (Result==1)//CHECK
    			printMessage(output,CHECK);
    
    		if (Result==2 || Result==3)//NO_CHECK
    			printMessage(output,NO_CHECK);
    	}//end CHECK
    
    	
    	else if (strcmp(command,"PRINT_BOARD")==0)
    		PrintBoard(output);
    	else if (strcmp(command,"EXIT")==0)
    		return;
    	else{
    		printMessage(output,WRONG_ARGUMENTS);
    		continue;
    		}
    }//end While
    if you can help me and take a look

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by c9dw2rm8 View Post
    in My program i need somtimes to find the word CHECK
    and sometime CHECKMATE
    maybe because both of them begin with the same sub string CHECK it's make problems?
    Quote Originally Posted by matsp View Post
    Only if you don't compare correctly.

    --
    Mats
    In other words, if you post code we can try to help you find errors, but:

    Code:
    strcmp("check", my_string_ptr);
    will return 0 if my_string_ptr is "check" and some negative number if it's "checkmate" while
    Code:
    strcmp("checkmate", my_string_ptr);
    will return positive and 0, respectively.
    Last edited by tabstop; 03-12-2008 at 10:09 AM. Reason: got my + and - backwards

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by c9dw2rm8 View Post
    Code:
    while (fgets(line,LINE_LENGTH,input)!=NULL){
    	command=strtok(line," \t\n\r");
    	if (command==NULL) continue; //empty line
    	if (strcmp(command,"NEW")==0){
    		tool=strtok(NULL," \t\n\r");
    		to=strtok(NULL," \t\n\r");
    		color=strtok(NULL," \t\n\r");
    		if (color==NULL || tool==NULL || to==NULL){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue;
    		}
    		AddPiece(color,tool,to,output);
    
    	}//end NEW
    
    	else if (strcmp(command,"MOVE")==0){
    		from=strtok(NULL," \t\n\r");//from
    		to=strtok(NULL," \t\n\r");//to
    		if ((from==NULL)||(to==NULL)){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue;
    		}
    		MoveTool(from,to,output);
    	}//end MOVE
    
    	else if(strcmp(command,"CHECKMATE")==0){
    		color=strtok(NULL," \t\n\r");
    		if (color==NULL){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue; //end the current session of the loop
    		}
    		CheckCheckMate(color,dest);
    		
    	}//end CHECKMATE
    
    
    	else if(strcmp(command,"CHECK")==0){
    		color=strtok(NULL," \t\n\r");
    		Result=CheckCheck(color,output);
    		if (color=NULL){
    			printMessage(output,WRONG_ARGUMENTS);
    			continue;
    		}
    		if (Result==0)//Not good Color
    			printMessage(output,NO_SUCH_COLOR);
    	
    		if (Result==1)//CHECK
    			printMessage(output,CHECK);
    
    		if (Result==2 || Result==3)//NO_CHECK
    			printMessage(output,NO_CHECK);
    	}//end CHECK
    
    	
    	else if (strcmp(command,"PRINT_BOARD")==0)
    		PrintBoard(output);
    	else if (strcmp(command,"EXIT")==0)
    		return;
    	else{
    		printMessage(output,WRONG_ARGUMENTS);
    		continue;
    		}
    }//end While
    if you can help me and take a look
    I'm a little unclear as to context, since I wouldn't expect that your chess program users would actually type "CHECK WHITE" or whatever -- or are you checking output of another chess program? -- but that's the way it's supposed to work. I notice that CheckCheckMate takes different arguments than everything else.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Location
    N?A
    Posts
    23
    Quote Originally Posted by tabstop View Post
    I'm a little unclear as to context, since I wouldn't expect that your chess program users would actually type "CHECK WHITE" or whatever -- or are you checking output of another chess program? -- but that's the way it's supposed to work. I notice that CheckCheckMate takes different arguments than everything else.
    Thanks for the problem in the checkmate i wrote dest insted of output ans it's worked?
    mmmmm.... intresting?

    Thank's anyway

  15. #15
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Quote Originally Posted by c9dw2rm8 View Post
    Hello!
    I'm trying to read from files some lines?
    I also use the strtok method to get words from each line!
    I need to give warning if there is too many parameters(word) or too little
    How i can do that ?
    Without strtok? maybe some kind of command or something like that?
    If you happen to get a hold of the "White book" the K&R C Programming Language book, section 1.5.4 example code (Word Counting) will perform what you need in terms of counting the number of words among other atributes. This is very similar to the 'wc' command on Unix.

    The book is pretty much a must have, by the way.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 04:48 PM
  2. a question about strtok.
    By Masterx in forum C++ Programming
    Replies: 24
    Last Post: 11-18-2008, 10:09 PM
  3. strtok question
    By neandrake in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2003, 02:51 AM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 08:22 PM

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