file reading

This is a discussion on file reading within the C Programming forums, part of the General Programming Boards category; Hey guys could someone explain as to why my program would segfault if any of my data files where missing ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    file reading

    Hey guys could someone explain as to why my program would
    segfault if any of my data files where missing that im trying to read. Im getting a segfault at fgets.

    To test if the file exists can i just do what iv done below?
    if(fp1 == NULL)
    {
    printf("file error does not exist\n");

    }

    Code:
    int loadData(GJCType* menu, char* menuFile, char* submenuFile)
    {
    
       CategoryType *currentCat, *prevCat, *newCat;
       ItemType *currentItem, *prevItem, *newItem;
       
       FILE *fp1;
       FILE *fp2;
       
       char *token, *line;
       char array[BUFFER_SIZE + 2];
       double d;
       unsigned doll;
       int i;
       char nc_array[NC_ARRAY_SIZE + 1];
       
       /* Open menu file for reading. */
       fp1 = fopen(menuFile, "r");
       
       /* check if fp1 menu file exists*/
       if(fp1 == NULL)
       {
          printf("file error\n");
          
       }
       
       /* initialize the previous node to NULL*/
       prevCat = NULL;
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp1)) != NULL)
       {
          /* allocate memory for CategoryType pointer*/
          newCat = malloc(sizeof(CategoryType));
          
          /* check if memory allocation succeeded if fails exit*/
          if(newCat == NULL)
          {
              printf("Memory Allocation error for newCat\n");
    	  exit(0);
          }
          
          /* if the prevCat is NULL point the new node to the 
          start of the list*/
          if(prevCat == NULL)
          {
             menu->headCategory = newCat;
          }
          /* if it isnt at the start get the next node*/
          else
          {
             prevCat->nextCategory = newCat;
          }
           
          
          /* tokenize the Pipe and copy the field
          pointers into the struct*/
               
          token = strtok(line, "|");
          strcpy(newCat->categoryID, token);
               
          token = strtok(NULL, "|");   
          newCat->drinkType = token[0];
          
          token = strtok(NULL, "|");
          strcpy(newCat->categoryName, token);
            
          token = strtok(NULL, "|");
          strcpy(newCat->categoryDescription, token);
       
          /* initialize everything to a safe state*/
          newCat->nextCategory = NULL;
          newCat->headItem = NULL;
          newCat->numItems = 0;
          prevCat = newCat;
       }
       
       /* the current pointer points to headCategory*/
       currentCat = menu->headCategory;
       
       /* for testing purposes traverse through the list
       and print out linked list*/
       while(currentCat != NULL)
       {
          printf("\n%s, %c, %s, %s", currentCat->categoryID, currentCat->drinkType,
          currentCat->categoryName, currentCat->categoryDescription);
          currentCat = currentCat->nextCategory;
       }
       
       /* close the first pointer to the file*/
       fclose(fp1);
       
       /* Open submenu file for reading. */
       fp2 = fopen(submenuFile, "r");
       
       /* check if sub menu file exists*/
       if(fp2 == NULL)
       {
          printf("file error\n");
          
       }
       /*intialize pointer to the start*/
       prevItem = NULL;
       currentCat = menu->headCategory;
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp2)) != NULL)
       {
          newItem = malloc(sizeof(ItemType));
        
          token = strtok(line, "|");
          strcpy(newItem->itemID, token);
          
          token = strtok(NULL, "|");
          strcpy(nc_array, token);
           
          /* put the current pointer to the head of the list*/
          currentCat = menu->headCategory;
          
          /* while current is pointing to a node*/
          
          while(currentCat != NULL)
          {
             if(strcmp(currentCat->categoryID, nc_array) == 0)
    	 {
    	    break;
    	 }
    	 
    	 currentCat = currentCat->nextCategory;
          }
    	 
    	 if(currentCat == NULL)
    	 {
    	    printf("Problem with data!!!\n");
    	 } 
    	 else
    	 {
                token = strtok(NULL, "|");
                strcpy(newItem->itemName, token);     
          
                for(i = 0; i < NUM_PRICES; i++)
                { 
                   token = strtok(NULL, "|");
                   d = atof(token); 
                   doll = (unsigned) d;  
                   newItem->prices[i].dollars = doll;
                   newItem->prices[i].cents = (d - doll) * 100;
                }
          
                token = strtok(NULL, "|");
                strcpy(newItem->itemDescription, token);
          
    /*
                if(it_prev == NULL)
                {
                   ct_curr->headItem = new_item;
                }  
          
                else
                {
                   it_prev->nextItem = new_item;
    	    }      
    
                new_item->nextItem = NULL;
                it_prev = new_item;
          
                new_item->nextItem = ct_curr->headItem;
                ct_curr->headItem = new_item;	    
    */
                newItem->nextItem = currentCat->headItem;
                currentCat->headItem = newItem;
    	    currentCat->numItems ++;
    	 }
          
       }
       currentCat = menu->headCategory;
       
       while(currentCat!= NULL)
       {
          currentItem = currentCat->headItem;
          printf("Items for category %s\n", currentCat->categoryID);
       
          while(currentItem != NULL)
          {
             printf("\n%s, %s, %s, %d.%d, %d.%d, %d.%d, %s\n", 
             currentItem->itemID, currentCat->categoryID,
             currentItem->itemName, currentItem->prices[0].dollars,currentItem->prices[0].cents,
             currentItem->prices[1].dollars, currentItem->prices[1].cents,
             currentItem->prices[2].dollars, currentItem->prices[2].cents,
             currentItem->itemDescription);
             currentItem = currentItem->nextItem;
          }
          
          currentCat = currentCat->nextCategory;  
       }
       fclose(fp2);
       
       return EXIT_SUCCESS;
    
    
    }

  2. #2
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    sorry guys fixed it

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Your problem was this, I imagine:
    Code:
       /* Open menu file for reading. */
       fp1 = fopen(menuFile, "r");
       
       /* check if fp1 menu file exists*/
       if(fp1 == NULL)
       {
          printf("file error\n");
         /* should exit the function */ 
       }
       
       /* initialize the previous node to NULL*/
       prevCat = NULL;
       
       while((line = fgets(array, BUFFER_SIZE + 2, fp1)) != NULL)
       {
    You don't free any of your data.
    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. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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