Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing linked list to binary file

Status
Not open for further replies.

chachi

Programmer
May 4, 2002
8
0
0
US
First of all, sorry for the profuse code that follows, but I don't want to post too little info. I'm trying to print a linked list that is associated with a binary file. The linked list has only the partid, offset and next pointer.
I can only get my print routine to print the last record. What have I done wrong? Also, I want the user to be able to specify a partid and view the other fields associated with the partid but I'm confused as to how to make the correlation between the partid in the linked list and rest of the record that is in the array filled by the fread. Any thoughts would be appreciated.

Code:
main()
{
   LISTNODEPTR startPtr = NULL;
   FILE *fp;
   struct invent item;
   long recoff;
   int choice;
   char partid[7];

   if((fp = fopen("PartList.Dat","rb")) != NULL)
   {
    recoff = ftell(fp);
    fread(&item, sizeof(struct invent),1,fp);
    do
     {
      if(!feof(fp))
      {
       insert_node(&startPtr, item.part, recoff);
       recoff = ftell(fp);
       fread(&item, sizeof(struct invent),1,fp);
      }
     }while(! feof(fp));
	 instructions();
	 printf("? ");
	 scanf("%d", &choice);
	 while (choice != 4) {
	   switch (choice) {
		  case 1:
			printf("\n\nThe list before deletions: \n");
			printList(startPtr);
			printf("Enter the Part ID you want deleted: ");
			scanf("%s", &partid);
			delete_node(&startPtr, partid);
			printf("\n\nThe list after deletions: \n");
			printList(startPtr);
			break;
		  case 2:
			printf("Enter the Part ID you're looking for: ");
			scanf("%s", &partid);
			search_list(startPtr, partid);
			break;
		  case 3:
			copy_file(&startPtr, item);
			break;
		  default:
			printf("Invalid choice.\n\n");
			instructions();
			break;
		}
	    printf("? ");
		scanf("%d", &choice);
	 }
   }
   else
    perror("Unable to open input file");
   //printList(startPtr);
   printf("End of run.\n");

   return 0;
}

void insert_node(LISTNODEPTR *sPtr, char *value, long roffst)
{
   LISTNODEPTR newPtr, previousPtr, currentPtr;

   newPtr = (LISTNODEPTR)malloc(sizeof(LISTNODE));

   if (newPtr != NULL) {    /* is space available */
      strcpy(newPtr->part,value);  
      newPtr->offset = roffst;  
      newPtr->nextPtr = NULL;  

      previousPtr = NULL;  
      currentPtr = *sPtr;  
      while (currentPtr != NULL && strcmp(currentPtr->part,value) < 0) {
       previousPtr = currentPtr;          
       currentPtr = currentPtr->nextPtr;  
      }

      if (previousPtr == NULL) {
       newPtr->nextPtr = *sPtr;
       *sPtr = newPtr;
      }
      else {
		  previousPtr->nextPtr = newPtr;
		  newPtr->nextPtr = currentPtr;
      }
   }
   else
      printf(&quot;%s not inserted. No memory available.\n&quot;, value);
   return;
}

int isEmpty(LISTNODEPTR sPtr)  {
   return sPtr == NULL;  }

/* Print the list */
void printList(LISTNODEPTR currentPtr)
{
   if (isEmpty(currentPtr)) 
      printf(&quot;List is empty.\n\n&quot;);
   else {
      printf(&quot;The list is:\n&quot;);

      while (currentPtr != NULL) {
       printf(&quot;%s --> &quot;, currentPtr->part);
       currentPtr = currentPtr->nextPtr;
      }
      printf(&quot;NULL\n\n&quot;);
   }
   return;
}

LISTNODE *
search_list(LISTNODEPTR sPtr, char *partid)
{
	LISTNODEPTR currentPtr;  
    for (currentPtr = sPtr;
	     currentPtr != NULL && strcmp(currentPtr->part,partid) != 0;
		 currentPtr = currentPtr->nextPtr) {}
    //add code to display info about part id
	return currentPtr;
}

void instructions(void)
{
	printf(&quot;Enter your choice:\n&quot;
		&quot;   1 to delete an element from the list.\n&quot;
		&quot;   2 to search for an inventory item.\n&quot;
		&quot;   3 to copy list to data file.\n&quot;
		&quot;   4 to exit\n&quot;);
}

int copy_file (LISTNODEPTR *sPtr, struct invent print_item)
{
   FILE *outp;
   LISTNODEPTR currentPtr, previousPtr;

   if ((outp = fopen(&quot;PartListCopy.dat&quot;, &quot;wb&quot;)) != NULL)
   {
	 for (currentPtr = sPtr;
	     currentPtr != NULL;
		 currentPtr = currentPtr->nextPtr)
	{
	   fwrite(&print_item, sizeof(struct invent),1, outp);
    }
	 fclose(outp);
	 return 1;
   }
   else
	 return 0;
}
 
Please disregard the second half of the question dealing with displaying the rest of the records. I figured it out.
 
Hi,

I could be totally off on this - just a guess...

Check this...

printList(startPtr);

By my accounts, you just got done assigning startPtr the address of the last record in your file with this iterated call...

insert_node(&startPtr, item.part, recoff);

Meaning, you are sitting at the last node in your list at this point already. So, you will display the one node and that's all. You should just be starting at the head and printing the list while the Next pointer isn't NULL. Try stepping through with the debugger if you have one - that always helps me.

I see in your question that you don't have a head in your linked list. Although I am a bit new to the linked list, I've always seen it done with a Node* head. The Node structure would have the Node* Next in it, not the list.

Sort of off the subject - you should close your file when you are done reading it. fclose(fp); Simple, and a good practice to get into.

Hope that helped.
-Tyler
 
Thanks. Yeah, I did forget to include the fclose. I was actually finally able to come up with a solution.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top