Bus error when reading input from file

This is a discussion on Bus error when reading input from file within the C Programming forums, part of the General Programming Boards category; Hello to all, I am receiving a bus error when I run the following code: Code: #include <stdio.h> #include <string.h> ...

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

    Bus error when reading input from file

    Hello to all,

    I am receiving a bus error when I run the following code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    typedef struct { char *name ; char *data ; int length ; int type ; } sequence ;
    
    sequence loadedSequences[1000] ;
    
    int nSequences = 0 ;
        
    int main()
        {
            char buffer[1000] ;        
            char *command ;
            char *filename, *list_num ;
            
            
            for(;;)
                {
                    printf("SeqTool> ") ;
                    fpurge(stdin) ;
                    
                    fgets(buffer, 1000, stdin) ;
                    fpurge(stdin) ;
                    
                    // Eliminating the newline character
                    char *p = strchr(buffer, '\n') ;
                    if (p) 
                        {
                            *p = '\0' ; 
                        } 
                                              
                    command = strtok( buffer, " \t\n" ) ; 
                    
    ////////////////// Load sequence in FASTA format //////////////////////////
                    
                    if( (strcmp ( command, "read" )) == 0 )
                        {
                            char *fileName ;
                            fileName = strtok(NULL, " \t\n" ) ;
                            FILE *input = fopen(filename, "r") ;
                            fgets( buffer, 1000, input) ;
                            char *seqName ;
                            seqName = &buffer[1] ;   // After the > in FASTA format  
                            
                            loadedSequences[nSequences].name = 
                                (char *) malloc( ( strlen(seqName) + 1 ) * sizeof( char ) ) ;
                                
                            strcpy( loadedSequences[nSequences].name , seqName ) ;
                            
                            ++nSequences ;
                   
                        }
    What I am trying to do is read in from a standardized format (FASTA) and collect the data so that I can manipulate it later if needed. The syntax for the command should be:

    SeqTool> read <filename>

    Any help would be great.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What if "command" after strchr() is NULL?

    I don't know if that is the problem, but it's certainly candidate.

    --
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    what if fopen failes? what if malloc failes?

    and do you know what will happen if fgets failes?

    You do not check return value of any function you call. So why are you so surprised that you have bus errors? You just asking for them...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    You're also creating variables in the middle of your code, variables that have an eerily similar name to something that you've already defined at the top of your main(). That might not be your problem, either, but it would certainly lead to confusion for me if this was my program.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    77
    I now have a new problem.

    I want to set a condition so that if my input file doesn't exist, it will print an error message. As of now I am getting a "bus error". However, if i read in a file that exist, I get no problems.
    Here is the code:

    Code:
    for(;;)
                {
                    printf("SeqTool> ") ;
                    fpurge(stdin) ;
                    
                    fgets(std_input, 1000, stdin) ;
                    fpurge(stdin) ;
                    
                    // Eliminating the newline character
                    char *p = strchr(std_input, '\n') ;
                    if (p) 
                        {
                            *p = '\0' ; 
                        } 
                                              
                    command = strtok( std_input, " \t\n" ) ; 
                    
    ////////////////// Load sequence in FASTA format //////////////////////////
                    
                    if( (strcmp ( command, "read" )) == 0 )
                        {
                            
                            filename = strtok(NULL, " \t\n" ) ; // collect filename
                            
                            if( ! (input = fopen(filename, "r"))) 
                                { 
                                    printf("COULD NOT OPEN FILE") ;
                                }
                           
                   
                        }
    The format for the command should look like this:
    SeqTool> read <filename>

    Any help would be great

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       static const char whitespace[] = " \t\n";
       for ( ;; )
       {
          char line[BUFSIZ];
          fputs("SeqTool> ", stdout);
          fflush(stdout);
    
          if ( fgets(line, sizeof line, stdin) )
          {
             char *command = strtok(line, whitespace);
             if ( command )
             {
                if ( strcmp(command, "exit") == 0 )
                {
                   break;
                }
                else if ( strcmp(command, "read") == 0 )
                {
                   char *filename = strtok(NULL, whitespace);
                   if ( filename )
                   {
                      FILE *file = fopen(filename, "r");
                      if ( !file )
                      {
                         perror(filename);
                         continue;
                      }
                      /* ... */
                      printf("opened &#37;s\n", filename);
                      fclose(file);
                   }
                }
             }
          }
       }
       return 0;
    }
    
    /*
    SeqTool> read foobar
    foobar: No such file or directory
    SeqTool> read file.txt
    opened file.txt
    SeqTool> exit
    */
    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
    Thanks for the help. Running fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. Replies: 7
    Last Post: 02-02-2009, 06:27 AM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM

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