Bus error

This is a discussion on Bus error within the C Programming forums, part of the General Programming Boards category; Hello to all, Unable to locate the source of my error. Any help would be great. Here is the code: ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    77

    Bus error

    Hello to all,

    Unable to locate the source of my error. Any help would be great. Here is the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
        
    int main()
        {
            char input[1000] ;        
            char *firstWord, *command ;
            
            for(;;)
                {
                    printf("SeqTool> ") ;
                    fgets(input, 1000, stdin) ;
                            
                    firstWord = strtok( input, " \t" ) ;
                    command = strtok( NULL, " \t" ) ;    
                    
                    // Load sequence in FASTA format
                    if(strcmp ( command, "read" ))
                        {
                            printf("Not available");
                        }
                        
                    // Write sequence in FASTA format
                    else if (strcmp ( command, "write"))
                        {
                            printf("Not available");
                        }
                        
                    // Display a detailed report for a selected sequence
                    else if (strcmp ( command, "show"))
                        {
                            printf("Not available");
                        }
                        
                    // Display a summary list of loaded sequences
                    else if (strcmp ( command, "list"))
                        {
                            printf("Not available");
                        }
                        
                    // Align two nucleotide sequences
                    else if (strcmp (command, "align"))
                        {
                            printf("Not available");
                        }
                    
                    // Quit
                    else if (strcmp (command, "quit"))
                        {
                            printf("Not available");
                        }
                    
                    // Any other command
                    else
                        {
                            printf("Improper command");
                        }
                    
                    continue ;
                }
        
        return(0) ;
        }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    strcmp returns zero when strings match.
    If strtok fails, you may be dereferencing a null pointer.
    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
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    How can I set a test for certain keywords using strcmp?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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.*

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Thanks for the link. However, I am still receiving a bus error though.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Are you checking the return values of the calls to strtok?
    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
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    What do you mean. Sorry, I am a newbie.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    I might choose to precede the if tree with this:
    Code:
                    if ( !firstWord || !command )
                    {
                       continue;
                    }
    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
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    Maybe it will help if I tell you what I am trying to do. I want the program to act like a command line interface. I am trying to use strtok to take the first word of the command so that I can jump to the code that is activated by that keyword. for instance:

    Seqtool> read <filename>

    I want to capture the token "read" and the start performing the if loop for that command.

    Sorry if I am confusing.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The first word would be firstWord, no?
    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.*

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM

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