strcmp help!!

This is a discussion on strcmp help!! within the C Programming forums, part of the General Programming Boards category; my program crashes at the condition part Code: /* Reading from the script file */ #include <stdio.h> #include <stdlib.h> #include ...

  1. #1
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9

    strcmp help!!

    my program crashes at the condition part

    Code:
    /* Reading from the script file */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    //#include <iostream>
    #include <string.h>
    
    int main()
    {
       FILE *myFile;
       char filename[20];
    
          
       /* Ask user to enter the filname including the format */
       printf( "Enter the script filename with the format. ie script.txt\n" );
       scanf( "&#37;s", filename  );
       
       /* Open filename for Reading (only) */
       myFile = fopen(filename, "r");
    		if (myFile == NULL) {
    			printf("ERROR! Unable to open your file.\n");   
                printf( "Closing file.......\n" );   
                //delay two second
                sleep (2000);
    			exit(-1);
       }    
       /* Open file success message */
            else {
                 printf( "The file %s is valid\n\n", filename );
    }
                 
                 
        char string[500];
        fgets (string , 500 , myFile);
    
        
        char *tokenPtr;
        tokenPtr = strtok( string , " " );
          while ( tokenPtr != NULL ) { 
          printf( "%s\n", tokenPtr );
          tokenPtr = strtok( NULL, " " );
    }
    
    
         //EXE Crashes here
     if ( strcmp ( tokenPtr, "RESET" ) == 0 ){
          printf ("\n Resetting the motors.");
          //outportb(0x378, 0); 
          }
          
    
    
    
       getch();
       return 0;
    }
    Last edited by apm; 06-05-2007 at 08:16 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    If the while loop preceding it doesn't end until tokenPtr is null, then what is tokenPtr when you pass it to strcmp?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    It looks like you're compiling your C code with a C++ compiler as well.
    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.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    1
    In

    Code:
        char *tokenPtr;
        tokenPtr = strtok( string , " " );
        while ( tokenPtr != NULL ) 
       { 
          printf( "%s\n", tokenPtr );
          tokenPtr = strtok( NULL, " " );
         }
    if it does not find SPACE or " ", tokenPtr will be NULL and hence in strcmp gets (NULL, RESET)...

  5. #5
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    it crashes after it prints out the the script from the file word by word

    Code:
    /* Reading from the script file */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    //#include <iostream>
    #include <string.h>
    //#include "DLPORTIO.h"
    
    int main()
    {
       FILE *myFile;
       char filename[20];
       int time[10] = {1,2,3,4,5,6,7,8,9,10};
       char data[50];
       char *token;
                 
                 
      /* Ask user to enter the filname including the format */
       printf( "Enter the script filename with the format. ie script.txt\n" );
       scanf( "&#37;s", filename  );
       
       /* Open filename for Reading (only) */
       myFile = fopen(filename, "r");
    		if (myFile == NULL) {
    			printf("ERROR! Unable to open your file.\n");   
                printf( "Closing file in 2 seconds.......\n" );   
                //delay two second
                sleep (2000);
    			exit(-1);
       }    
       /* Open file success message */
            else {
                 printf( "The script file %s is being read....\n\n", filename );
    }
                 
        fgets (data , 50 , myFile);
        
        token = strtok( data , " " );
        while ( token != NULL ) 
       { 
          printf( "%s\n", token );
          token = strtok( NULL, " " );
       
    }
          //I GUESS IT CRASHES HERE?
    
          //token=toupper(token);
           if ( strcmp ( token, "RESET" )==0 ) {
               printf( "Resetting the motor ......");
          //outportb(0x378, 0x00); 
          }
          else if ( strcmp ( token, "#" )== 0 ){
               printf( "Skipped..");
               sleep (500);
               exit(-1);
               }
          else if ( strcmp ( token, "FORWARD" )==0 ) {
               printf( "Moving forward.......");
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "REVERSE" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "PAUSE" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "HEADLIGHTS" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "TURNRIGHT" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "TURN LEFT" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "LOOP" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "ENDLOOP" )==1 ) {
          //outportb(0x378, 0x01); 
               }
          
       getch();
       return 0;
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    http://www.open-std.org/jtc1/sc22/wg...rary_functions
    7.1.4 Use of library functions
    1 Each of the following statements applies unless explicitly stated otherwise in the detailed descriptions that follow: If an argument to a function has an invalid value (such as a value outside the domain of the function, or a pointer outside the address space of the program, or a null pointer, or a pointer to non-modifiable storage when the corresponding parameter is not const-qualified) or a type (after promotion) not expected by a function with variable number of arguments, the behavior is undefined.
    Are you not pretty much all but guaranteeing a crash in the best case?

    Don't pass a null pointer to strcmp.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Don't pass a null pointer to strcmp.
    what kind of loop can i use so i can pass it to strcmp then?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I dunno. My crystal ball is out for cleaning and I can't visualize your input file or intentions.

    A guess might be to put your if-tree in the loop instead of after it. But I'm not psychic either.

    Perhaps learn to indent better?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Quote Originally Posted by Dave_Sinkula View Post
    I dunno. My crystal ball is out for cleaning and I can't visualize your input file or intentions.

    A guess might be to put your if-tree in the loop instead of after it. But I'm not psychic either.

    Perhaps learn to indent better?
    sorry about the indent

    purpose of the program is to read a script from a text file, then tokenize each word/number, then use strcmp to read the word/number that has been tokenized to outport parallel pins.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Well then take Dave_Sinkula's suggestion and put the if(strcmp()) inside the while loop, before the call to strtok(). Right after the printf() would be a good place.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Quote Originally Posted by dwks View Post
    Well then take Dave_Sinkula's suggestion and put the if(strcmp()) inside the while loop, before the call to strtok(). Right after the printf() would be a good place.
    it works but it crashes
    heres a screenshot
    http://img362.imageshack.us/img362/2691/untitlednx5.jpg


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    #include <ctype.h>
    //#include <iostream>
    #include <string.h>
    //#include "DLPORTIO.h"
    
    int main()
    {
       FILE *myFile;
       char filename[20];
       int time[10] = {1,2,3,4,5,6,7,8,9,10};
       char data[50];
       char *token;
                 
                 
       /* Ask user to enter the filname including the format */
       printf( "Enter the script filename with the format. ie script.txt\n" );
       scanf( "%s", filename  );
       
       /* Open filename for Reading (only) */
       myFile = fopen(filename, "r");
    		if (myFile == NULL) {
    			printf("ERROR! Unable to open your file.\n");   
                printf( "Closing file in 2 seconds.......\n" );   
                //delay two second
                sleep (2000);
    			exit(-1);
            }    
       /* Open file success message */
            else {
                 printf( "The script file %s is being read....\n\n", filename );
            }
                 
       /* Reading data from script file */   
        fgets (data , 50 , myFile);
        token = strtok( data , " " );
        
       /* Looping the token for output until it's null(empty) */
        while ( token != NULL ) 
       { 
          printf( "%s\n", token );
          
          if ( strcmp ( token, "RESET" )==0 ) {
               printf( "Resetting the motor ......");
          //outportb(0x378, 0x00); 
          } 
          else if ( strcmp ( token, "#" )== 0 ){
               printf( "Skipped..");
               sleep (500);
               exit(-1);
          }
          else if ( strcmp ( token, "FORWARD" )==0 ) {
               printf( "Moving forward.......");
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "REVERSE" )==0 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "PAUSE" )==0) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "HEADLIGHTS" )==0 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "TURNRIGHT" )==0 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "TURN LEFT" )==0 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "LOOP" )==0 ) {
          //outportb(0x378, 0x01); 
               }
          else if ( strcmp ( token, "ENDLOOP" )==0 ) {
          //outportb(0x378, 0x01); 
          }
          
          // Ends when the script file is null
          token = strtok( NULL, " " );
    }
          
    
          printf( "\n Press any key to end program....." );
          getch();
          return 0;
    }

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by apm View Post
    it works but it crashes
    Maybe it doesn't know what to do with getch?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    thanks for the help! i decided to make new a new txt for the heck of it and it doesn't crash anymore, strange.

    3 more things to do until my program is complete

    - make the script uppercase right when the script has been tokenized so my condition will work, i tried to use toupper but it gives me error
    Code:
     token=toupper(token);
    
    aa.c:41: warning: passing arg 1 of `toupper' makes integer from pointer without a cast
    aa.c:41: warning: assignment makes pointer from integer without a cast
    - how do i make it so that if a line in the script file starts with a symbol ie "#, $" then it skips the whole line?




    - if a line starts with a LOOP command and follow by a number 2 then i have to make a loop about this command 2 times, instead other lines are just "FORWARD 2" How do i do this?
    example
    Code:
    LOOP 3
    	FORWARD 2
    	Headlights ON
    	Pause 2
    	RESET
    EndLoop
    vs.
    Code:
    Forward 3


    hope someone can help me here!

    thanks
    Last edited by apm; 06-07-2007 at 06:40 PM.

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by apm View Post
    - make the script uppercase right when the script has been tokenized so my condition will work, i tried to use toupper but it gives me error
    Code:
     token=toupper(token);
    
    aa.c:41: warning: passing arg 1 of `toupper' makes integer from pointer without a cast
    aa.c:41: warning: assignment makes pointer from integer without a cast
    toupper operates on a single character, not a string. You can write a trivial function to do a whole string.

    Quote Originally Posted by apm View Post
    - how do i make it so that if a line in the script file starts with a symbol ie "#, $" then it skips the whole line?
    Check the first character, ignore the rest of the line.

    Quote Originally Posted by apm View Post
    - if a line starts with a LOOP command and follow by a number 2 then i have to make a loop about this command 2 times, instead other lines are just "FORWARD 2" How do i do this?
    example
    Code:
    LOOP 3
    	FORWARD 2
    	Headlights ON
    	Pause 2
    	RESET
    EndLoop
    vs.
    Code:
    Forward 3
    For now, work on the first two.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    apm
    apm is offline
    Registered User
    Join Date
    Jun 2007
    Posts
    9
    Quote Originally Posted by Dave_Sinkula View Post
    toupper operates on a single character, not a string. You can write a trivial function to do a whole string.

    Check the first character, ignore the rest of the line.

    For now, work on the first two.

    1. strupr( token ); worked like a charm

    2. actually if i can just make condition in which if they see symbol they'll just print out ignore line. It would be an escape, except if a line is like "# FORWARD"

    3. i presume that i have to make conditions within the condition but i'm kinda stuck
    Last edited by apm; 06-07-2007 at 07:03 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 05:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 07:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM

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