Bus error (Core Dumped)

This is a discussion on Bus error (Core Dumped) within the C Programming forums, part of the General Programming Boards category; Hey everyone, I am writing a basic program titled, "LMC.c" that takes the contents from a file "LMC.s", and outputs ...

  1. #1
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19

    Bus error (Core Dumped)

    Hey everyone,

    I am writing a basic program titled, "LMC.c" that takes the contents from a file "LMC.s", and outputs them into another file "LMC.o"

    Here is the input file, LMC.s

    Code:
    INP 00
    STO 90
    INP 00
    ADD 90
    OUT 00
    STOP 00

    Then here is the program that reads the input file, LMC.c

    Code:
    #include<stdio.h>   
    #include<string.h>   
    
    int main()
    {
    	FILE *input,*output; 
    	char opcode[5],address[5]; 
    	char binopcode[10], binaddress[10];
    	int i;
    
    	/* the following codes open the file for reading and the file for output */
    	input = fopen("LMC.s","r");
    	output = fopen("LMC.o","w");
    
    
    
    	while(fscanf(input,"%s %s",opcode,address)!=EOF)
    		{
    			printf("Opcode: %s \t\tAddress: %s\n",opcode,address);		
    
    		if(strcmp(opcode, "INP") == 0) 
    		{
    			strcpy(binopcode,"10000000");
    			strcpy(binaddress," ");
    		}
    		else if(strcmp(opcode,"STO") == 0) 
    		{
    			strcpy(binopcode,"00000011");
    			strcpy(binaddress,"01011010");
    		}
    		else if(strcmp(opcode,"ADD") == 0) 
    		{
    			strcpy(binopcode,"00000001");
    			strcpy(binaddress,"01011010");
    		}
    		else if(strcmp(opcode,"OUT") == 0) 
    		{
    			strcpy(binopcode,"01000000");
    			strcpy(binaddress,"");
    		}
    		else if(strcmp(opcode,"STOP") == 0) 
    		{
    			strcpy(binopcode,"00000000");
    			strcpy(binaddress,"");
    		}
    		/* now put the results to the output file */
    
    		fprintf(output,"%s %s\n",binopcode,binaddress);
    		printf( "%s %s\n",binopcode,binaddress);
    		
    
    		} /*end while*/
    
    	
    	fclose(output);
    	fclose(input);
    		
    	return 0;
    	}

    I cannot get it to output the file into "LMC.o" though. I keep getting a "Bus error (core dumped)" when I compile it. Any suggestions why this might be?

    Thanks,

    MATT

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You get that error during the compilation or running your program? Can your program do anything correctly other than write to that file (can it read the input)?

    http://www.cplusplus.com/ref/cstdio/fopen.html

    You should check for the success of this function as it is a very likely source of errors.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  3. #3
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    i get the error during compilation, yes. There are no other errors during compilation. It does not create the LMC.o file at all... so I guess Im kinda stuck

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Okay, the compilation (or really, the build) process is the proces in which you turn your code into a program. Then, you have the ability to run your program after this happens. I get the feeling that you are actually running your program, that the error is seperate from the compilation process, and that it stems from you not checking the return values of fopen. Would you mind verifying these hypotheses?

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  5. #5
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    Yes, Im sorry. I wasnt thinking right.

    The program compiles perfectly. But when I run it, I get the Bus error. Thanks for straightening my out, lol

  6. #6
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    so with that being said.... any one have any new input to help me out here?

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Check your file handles for null values.

    ╔╗╔╦══╦╗╔╦══╦╗
    ║╚╝║╔╗║╚╝║╔╗║║
    ║╔╗║╠╣║╔╗║╠╣╠╣
    ╚╝╚╩╝╚╩╝╚╩╝╚╩╝

    codez http://code.google.com/p/zxcvbn/

  8. #8
    Registered User RandomX's Avatar
    Join Date
    Nov 2006
    Posts
    19
    Quote Originally Posted by Tonto
    Check your file handles for null values.
    Im not quite sure what that means. im fairly new to programming....

    Would you care to expand?

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    Code:
    	input = fopen("LMC.s","r");
    	output = fopen("LMC.o","w");
    Code:
    if ((input = fopen("LMC.s", "r")) == NULL)
    {
        perror("input file");
        exit(1);
    }
    if ((output = fopen("LMC.o", "w")) == NULL)
    {
        perror("output file");
        exit(1);
    }

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    I prefer the following, since it compacts error handling code:


    Code:
    FILE *input = fopen("LMC.s","r");
    FILE *output = fopen("LMC.o","w");
    
    if (input == NULL || output == NULL)
    {
      printf("error opening file: input %p output %p\n", (void *)input, (void *)output);
      return 1;
    }
    I know, just preference

  11. #11
    Registered User
    Join Date
    Nov 2006
    Posts
    176
    sure you could do that, but since fopen sets errno, perror is alot better than just printf, gives you the reason why there was an error. ie. no such file, invalid permission etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to generate a bus error
    By Tommo in forum C Programming
    Replies: 4
    Last Post: 08-22-2007, 05:37 AM
  2. Bus Error (rand)
    By nacho4d in forum C Programming
    Replies: 4
    Last Post: 06-22-2007, 04:38 AM
  3. Initializing array of structures causing bus error
    By Aaron M in forum C Programming
    Replies: 5
    Last Post: 03-05-2006, 12:40 AM
  4. Replies: 72
    Last Post: 11-25-2002, 04:55 PM
  5. PCI ISA bus Interface
    By Mohsinzb in forum C Programming
    Replies: 0
    Last Post: 03-27-2002, 12:12 PM

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