Thread: help! seg fault!

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    7

    help! seg fault!

    I have written a program that reads in words from a file "carol.txt" and stores them in a linked list, keeping a count of how many times each word occurs.

    When i try to run the program i get a segmentation fault. After some investigation I think it may have something to do with fopen in main().
    I don't have a debugger, so I'd be really grateful if someone could take a look at the code.

    Here it is, sorry it's so long!

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    #include <time.h>
    
    typedef struct L
    {
            char *word;
    	    int count;
            struct L *next;
    } list;
    
    
    list *makenode(char *a, list *t)
    {
        list *h = calloc(1, sizeof(list));
        h->word = a;
        h -> count = 1;
        h -> next = t;
        return h;
    }
    
    /* strcmp didn't work so i had to create function comparestr*/
    int comparestr (char *one, char *two)
    {
    	int i;
    	
    	for (i=0; i<50; i++)
    	{
    		if (one[i] == two[i])
    		{
    		return 0;
    		}
    	
    	}
    	return 1;
    }
    
    
    /* shows if word already exists in the list */
    list *findword(char *a, list *b)
    {
         while (1)
         {
    	if (b->next==NULL)
    	{
    		return b;
    	}
    	else if (comparestr(a, b->next->word)==0)
    	{
    		(b->next->count)++;
    		return NULL;
    	}
    	else
    	{
    		return (findword(a, b->next));
    	}
        }
    }
    
    
    char *readword(FILE *file)
    {
    	int n;
    	char *word;
    	word = calloc(50, sizeof(char));
    
    	word[0] = tolower(fgetc(file));
    	while (islower(word[0])||word[0]==EOF)
    	{
    		word[0] = tolower(fgetc(file));
    	}
    
    	if (word[0]==EOF)
    	{
    		return NULL;
    	}
    	
    	n=1;
    	word[1] = tolower(fgetc(file));
    	while ((islower(word[n])||word[n]==39||word[n]==44||word[n]==96) && n<49)
    	{
    		n++;
    		word[n]=tolower(fgetc(file));
    	}
    
    	while (!(islower(word[n])))
    	{
    		word[n] = 0;
    		n--;
    	}
    
    	n++;
    	word[n]='\0';
    	return word;
    }
    
    list *makelist(FILE *file)
    {
    	char *word;
    	list *start= makenode(NULL, NULL);
    	list *temp=NULL;
    
    	while (1)
    {
    
    	word = readword(file);
    	
    	if (word == NULL)
    	{
    		return start;
    	}
    
    	temp=findword(word, start);
    
    	if (temp !=NULL)
    	{
    		temp->next = makenode(word, NULL);
    	}
    
    	if (temp==NULL)
    	{
    		free(word);
    	}
    }
    }
    	
    list *search (char *a, list *b)
    {
         while (1)
         {
    	if (b->next==NULL)
    	{
    		return NULL;
    	}
    	else if (comparestr(a, b->next->word)==0)
    	{
    		return b->next;
    	}
    	else
    	{
    		return (search(a, b->next));
    	}
        }
    }
    
    void results(list *h, char *ve)
    {
    	   if (h == NULL)
             {
                printf("WORD NOT FOUND \n");
    	    return;
             }
                printf(" %s occurs %d times\n", ve, h->count);
    }
    
    void query (list *li)
    {
    	char *nu;
    	printf("type the word you would like to search for \n");
    	nu = readword(stdin);
    	results(search(nu, li), nu);
    }
    	
    
    
    
    void close(list *ha)
    {
    	list *g;
    	list *j;
    
    	g = ha;
    	while (g->next !=NULL)
    	{
    		j=g->next;
    		free(g->word);
    		free(g);
    		g=j;
    	}
    	free(g->word);
    	free(g);
    }
    
    int main()
    {
    	list *start;
    	FILE *file;
    	char array[15];
    
    	file = fopen( "carol.txt" , "r" );
    
    	start = makelist(file);
    	
    	while (1)
    	{
    		printf("do you want to make a query?\n");
    		
    	fgets(array, 15, stdin);
    	
    	if (tolower(array[0])=='y')
    	{
    		query(start);
    	}
    
    	if (tolower(array[0])=='n')
    	{
    		fclose(file);
    		close(start);
    		return 0;
    	}
    
    	if (tolower(array[0])!='y')
    	{
    		printf("you have entered an incorrect answer please try again\n");
    	}
    	}
    }
    thanks a lot.

  2. #2
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Are you sure the file you are opening exists? Is it in the current working directory?

    Always check the return value of fopen. If fopen fails, "file" will be NULL, and you are trying to dereference a NULL pointer. Its a sure crash. Add a check to see that (file != NULL) and exit there if it is.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    7
    yes, the file definatly exists in the working directory and fopen is still returning NULL. any idea why?

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    134
    Does the file have read privilege? Do you own the file? Try giving the full path instead of just filename.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by 98holb
    yes, the file definatly exists in the working directory and fopen is still returning NULL. any idea why?
    I like to use this idiom so that the system can answer that question.
    Code:
       static const char filename[] = "carol.txt";
       FILE *file = fopen(filename, "r");
       if ( file != NULL )
       {
          /* do stuff */
          fclose(file);
       }
       else
       {
          perror(filename);
       }
    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.*

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This page contains all the possible reasons fopen() might fail: http://www.opengroup.org/onlinepubs/...xsh/fopen.html

    Usually it fails because the file doesn't exist or you don't have adequate permissions.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM