Thread: problem with segmentation fault

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    36

    problem with segmentation fault

    Hi..everyone..i had trying to solve this problem but i really can't because i don't have any experience with C core dumped.Below is a program that can be compile but after exceution i get Segmentation fault (core dumped) with gcc (cygwin) compiler. Can anyone give me some piece of advice or tips.Thanks alot.
    Code:
    typedef struct bookStruct
    {
       int id;
       char title[TITLE_LEN + 1];
       char author[AUTHOR_LEN + 1];
       int year;
       float price;
       int count;
       struct bookStruct* next;
    } BookType;
    
    typedef struct bookListStruct
    {
       BookType* head;
       int count;
    } BookListType;
    
    BookType *b;
     
    /* Function prototypes. */
    
    void loadData(BookListType*, char*);
    void displayBooks(BookListType*);
    
    //int main(void)
    int main(int argc, char *argv[])
    {
     BookListType* booklist;
     int i;
     FILE *bookRead;
     char line[LINESIZE] , *word;
     
     if ( argc != 2 ) 
     {
        fprintf ( stderr, "Usage: %s <readfile1>\n", argv[0] );
        exit ( EXIT_FAILURE );
     }
      if((bookRead = fopen( argv[1], "r" )) == NULL)
     {
      fprintf(stderr, "cat: can't open %s\n", argv[1]);
     }
      
      loadData(booklist, argv[1]);
      displayBooks(booklist);
      fclose ( bookRead );
      return EXIT_SUCCESS;
    }
    
    void loadData(BookListType* booklist, char* booksFile)
    {   
       char *delimeter = ":";
       char  *token, *bookPtr;
       char bookWord[100];
       BookType *b;
       FILE *bfile;
    	/* fopen opens the file; exits if file cannot be opened */
    	
    	if ( ( bfile = fopen( booksFile, "r" ) ) == NULL ) 
    	{
    		fprintf(stderr,"***> Open error reading input file %s", booksFile);
    		exit(-1);
    	} 
    	/* read all records from file (until eof) */
    	while(!feof(bfile))
    	{
    	  while (bookPtr != NULL)
    	   {
            if(fgets(bookWord,sizeof(bookWord),bfile)!=NULL) 
            {
     	      if (b = (BookType*)malloc(sizeof(BookType)))
               {   
                /*	copy the data from BookType to linked list	*/ 
                bookPtr = strtok(bookWord,delimeter);
                b->id=atoi(bookPtr);
    		    strcpy(b->title, bookPtr);
    		    strcpy(b->author, bookPtr);
    		    b->year = atoi(bookPtr);
    		    b->price = atof(bookPtr);
    		    b->count = atoi(bookPtr);
                }
                bookPtr = strtok(NULL, delimeter);
             }    
                b->next = booklist->head;
                booklist->head = b;
                booklist->count++;       
    	     }
            }
    	fclose(bfile);
    }
    void displayBooks(BookListType* booklist)
    {
         printf("ID  Title\t\t\t    Author\t Year\tPrice\tCount\n");
    	printf("--  -----------------------------   ------------ ----\t-------\t-----\n");
    
    	while(booklist->head!=NULL)
    	{
    		printf("%2d  %-32s%-12s%6d\t%2c%5.2f\t%4d\n", booklist->head->id,
    			booklist->head->title, booklist->head->author, booklist->head->year,'$', 
    			booklist->head->price,booklist->head->count);
    		booklist->head = booklist->head->next;
    	}
    }
    Last edited by cBegginer; 05-14-2005 at 09:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Strange segmentation fault
    By Ron in forum C Programming
    Replies: 24
    Last Post: 06-15-2008, 02:10 PM
  3. Segmentation Fault hmm?
    By pobri19 in forum C Programming
    Replies: 4
    Last Post: 05-03-2008, 07:51 AM
  4. Re: Segmentation fault
    By turkish_van in forum C Programming
    Replies: 8
    Last Post: 01-20-2007, 05:50 PM
  5. [C++] Segmentation Fault {Novice C++ Programmer}
    By INFERNO2K in forum C++ Programming
    Replies: 24
    Last Post: 06-08-2005, 07:44 PM