Thread: Segmentation Fault - in fgets () from /lib/lib.so.6

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Segmentation Fault - in fgets () from /lib/lib.so.6

    Hey,
    i've just started studying software engineering and i am having a lot of trouble with a program i wrote for an assignment. the program is just meant to read a file line by line tokenise the line, check it against certain parameters and then print before going onto the next line.

    after writing the program i got a segmentation fault, and then used gdb to see what was the problem. It compiles fine.

    Gdb returned this:
    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7ac22ad in fgets () from /lib/libc.so.6

    the code is as follows:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int AR = 100;
    char *item;
    char line[201]="asdf";
    int	a;
    int isbn[11];
    int first[100];
    char checked[20];
    
    int main()
    {
    	int i,sum,check,j,k,p;
    	printf("%-13s %-15s %-10s %-20s\n\n","ISBN Code","Check Digit","Sum","Validation Message");
    		
    	FILE *fileisbn;
    	fileisbn = fopen("isbn.txt","r");
    	int e =0, d=0;
    
    	/*while(fscanf(fileisbn,"%s",item))*/
     	while (fgets(line, 20, fileisbn))
    	{item=strtok(line,"-");
    	a = atoi(item);
    	a=isbn[0];
    	item=strtok(NULL,"");
    	a = atoi(item);
    	a=isbn[1];
    	item=strtok(NULL,"");
    	a = atoi(item);
    	a=isbn[2];
    	item=strtok(NULL,"");
    	a = atoi(item);
    	a=isbn[4];
    	item=strtok(NULL,"-");
    	a = atoi(item);
    	a=isbn[5];
    	item=strtok(NULL,"");
    	a = atoi(item);
    	a=isbn[6];
    	item=strtok(NULL,"");
    	a = atoi(item);
    	a=isbn[7];
    	item=strtok(NULL,"-");
    	a = atoi(item);
    	a=isbn[8];
    	item=strtok(NULL,"\n");
    	p=strcmp(item,"x");
    	
    	if (p=0)
    		{isbn[9]=10;}
    	else
    		a=atoi(item);	
    		a=isbn[9];
    	
    	
    		for(i=0;i<9;i++)
    			{sum+=isbn[i]*i;}
    		check=sum%isbn[9];
    	
    	if (check=isbn[9])
    		{strcpy(checked,"Validated");
    		d++;}
    	else
    		strcpy(checked,"Error in ISBN code");
    
    
    	printf("%d-",isbn[0]);
    	for(j=1;j<=3;j++)	
    		{printf("%d",isbn[j]);}
    	printf("-");
    	for(k=4;k<=8;k++)
    		{printf("%d",isbn[k]);}
    	if (p=0)
    		{printf("-x");}
    	else
    	printf("-%d",isbn[9]);
    	
    	printf("%-15d",isbn[9]);
    	printf("%-10d",sum);
    	printf("%-20s\n",checked);
    	e++;
    	
    
    
    }
    	printf("Total number of ISBN codes processed = %d",e );
    	printf("Total number of ISBN codes validated = %d",d);
    return;
    }
    As i only used fgets once it is safe to assume the line in which the error is occuring.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't actually check to see that fopen was successful. For all you know you are trying to fgets from a failed fopen.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Are you sure fileisbn is valid at the point fgets() is called? You should really check fopen()'s return value against NULL to see if the file opened successfully.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. seg fault with fgets!!!!
    By dustydude in forum C Programming
    Replies: 4
    Last Post: 06-24-2010, 06:50 AM
  2. Segmentation fault?
    By onako in forum C++ Programming
    Replies: 4
    Last Post: 04-26-2010, 10:51 PM
  3. segmentation fault
    By js_badboy in forum C Programming
    Replies: 2
    Last Post: 09-11-2003, 06:59 PM
  4. segmentation fault
    By newbie_grg in forum Linux Programming
    Replies: 5
    Last Post: 01-26-2003, 05:07 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM