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.
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("%s not inserted. No memory available.\n", value);
return;
}
int isEmpty(LISTNODEPTR sPtr) {
return sPtr == NULL; }
/* Print the list */
void printList(LISTNODEPTR currentPtr)
{
if (isEmpty(currentPtr))
printf("List is empty.\n\n");
else {
printf("The list is:\n");
while (currentPtr != NULL) {
printf("%s --> ", currentPtr->part);
currentPtr = currentPtr->nextPtr;
}
printf("NULL\n\n");
}
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("Enter your choice:\n"
" 1 to delete an element from the list.\n"
" 2 to search for an inventory item.\n"
" 3 to copy list to data file.\n"
" 4 to exit\n");
}
int copy_file (LISTNODEPTR *sPtr, struct invent print_item)
{
FILE *outp;
LISTNODEPTR currentPtr, previousPtr;
if ((outp = fopen("PartListCopy.dat", "wb")) != NULL)
{
for (currentPtr = sPtr;
currentPtr != NULL;
currentPtr = currentPtr->nextPtr)
{
fwrite(&print_item, sizeof(struct invent),1, outp);
}
fclose(outp);
return 1;
}
else
return 0;
}