I'm reading in a text file that contains a little custom script. However, one line of that script gets ignored and I can't figure out why.

Script:

# Test Case #1 - Simple Movement
RESET
FORWARD 2
PAUSE 1
REVERSE 2
PAUSE 2
TURNRIGHT 2
PAUSE 1
TURNLEFT 2
PAUSE 2
RESET

Program:

int main( void )
{

    /* Declare variables */
    FILE *file;
    char line[80];
    char *tokenPtr;
    char fileName[6];

    printf( "Enter the name of the file to be read.\n" );
    gets( fileName );
    printf( "\n" );

    file = fopen( fileName, "r" );

    /* Create "Test.txt" for reading */
    if ( file != NULL ) {
        /* While the end of the file has not been reached,
         read each line */
        while ( fgets ( line, sizeof line, file ) != NULL ) {

            /* Tokenize line with a space delimeter */
            tokenPtr = strtok( line, " " );
            
            /* While there are still more words to be read */
            while( tokenPtr != NULL ) {

                /* Convert token to upper case */
                *tokenPtr = toupper( *tokenPtr );

                /* If the character in the line is '#',
                 then the whole line is treated as a comment and ignored */
                if( strcmp( tokenPtr, "#\0" ) == 0 ) {
                    printf( "Comment encountered\n" );
                    break; /* Break out of the loop to ignore the rest of the line */
                }
                /* Otherwise, if the word to be read is "FORWARD", then call the corresponding function */
                if( strcmp( tokenPtr, "FORWARD" ) == 0 ) {
                    printf( "Forward\n" );
                }
                /* Otherwise, word is "REVERSE" */
                if( strcmp( tokenPtr, "REVERSE" ) == 0 ) {
                    printf( "Reverse\n" );
                }
                /* Otherwise, word is "PAUSE" */
                if( strcmp( tokenPtr, "PAUSE" ) == 0 ) {
                    printf( "Pause\n" );
                }
                /* Otherwise, word is "TURNRIGHT" */
                if( strcmp( tokenPtr, "TURNRIGHT" ) == 0 ) {
                    printf( "Turn right\n" );
                }
                /* Otherwise, word is "TURNLEFT" */
                if( strcmp( tokenPtr, "TURNLEFT" ) == 0 ) {
                    printf( "Turn left\n" );
                }
                /* Otherwise, word is "RESET" */
                if( strcmp( tokenPtr, "RESET" ) == 0 ) {
                    printf( "Reset\n" );
                }
                /* Otherwise, word is "LOOP" */
                if( strcmp( tokenPtr, "LOOP" ) == 0 ) {
                    printf( "Loop\n" );
                }
                /* Otherwise, word is "END" */
                if( strcmp( tokenPtr, "END" ) == 0 ) {
                    printf( "End loop" );
                }
                    
                /* Stop when tokenPtr == null */
                tokenPtr = strtok( NULL, " " );

            } /* End while( tokenPtr != NULL ) */

        } /* End while( fgets ( line, sizeof line, file ) != NULL ) */

    } else {
        printf( "Cannot open file." );
    }

    /* Wait for keypress, then exit */
    getch();
    return 0;

}

At first I thought it was just ignoring any line below a comment (line starting with #), but even when I removed the comment the program continued ignoring the existence of the first RESET line.

Thanks for any help.

fgets() stores newlines because the absence of a newline is how fgets() tells the caller that only a partial line was read. The comparison is failing because the line contains "RESET\n" instead of "RESET". The usual way to get around a problem like this is trimming the newline.

while ( fgets ( line, sizeof line, file ) != NULL ) {
    char *end = strchr(line, '\n');

    if (end) *end = '\0';

    /* Tokenize line with a space delimeter */
    tokenPtr = strtok( line, " " );
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.