Hello,
I am having a problem. The task I have been given is to implement a queue using a linked list. One of the functions of the program is it accept input from the user and write this to a file. The program, thus far, accepts the information, but prints garbage to the .txt file. I would really appreciate any assistance offered. Thank you. A section of the program has been added.
int main (void)
{
FILE *jPtr;
JobPtr headPtr = NULL;
JobPtr tailPtr = NULL;
int choice;
int number;
char item;
main_menu();
while(choice!=4)
{
switch(choice)
{
case 1:
system("cls");
addFunction(&headPtr,&tailPtr);
main_menu();
scanf("%d", &choice);
break;
case 2:
system("cls");
if(!isEmpty(headPtr))
{
item = removeFunction(&headPtr, &tailPtr);
printf("Job has been removed from list.\n");
}
main_menu();
scanf("%d", &choice);
break;
case 3:
system("cls");
displayFunction(headPtr);
main_menu();
scanf("%d", &choice);
break;
default:
printf("Invalid choice.\n");
system("cls");
main_menu();
scanf("%d", &choice);
break;
}
}
return 0;
}
/******************************************************************************/
/* addFunction */
/******************************************************************************/
void addFunction(JobPtr *headPtr, JobPtr *tailPtr)
{
FILE *jPtr;
int number;
JobPtr newPtr; /*pointer to new node*/
newPtr = malloc(sizeof(job)); /*create new node*/
//struct jobData jobs = {0,"","","","","","",""};
if ((jPtr = fopen("jobticket.txt", "w"))== NULL)
{
printf("File could not be opened\n");
}
fclose (jPtr);
if ((jPtr = fopen("jobticket.txt", "rb+"))==NULL)
{
printf("File could not be opened\n");
} /*end if*/
else
{
while (fwrite(newPtr,1, sizeof(*newPtr),jPtr)>0)
{
printf("Enter jobnum: ");
scanf("%d",&number);
fflush(stdin);
if (number == 0)
break;
fseek(jPtr, (number-1)* sizeof(struct jobData), SEEK_SET);
fread(newPtr, sizeof(*newPtr),1,jPtr);
newPtr->jobnum = number;
printf("Enter a brief description of problem: \n");
gets(newPtr->jissue);
printf("Enter worktype: \n");
gets(newPtr->wtype);
printf("Enter date. Enter in this format DD/MM/YY: \n");
gets(newPtr->date);
printf("Enter technician to which job will be assigned. "
"(technician chosen according to work location): \n");
gets(newPtr->techcode);
newPtr->nextPtr=NULL;
fwrite(newPtr,1,sizeof(struct jobData),jPtr); //fwrite(this, sizeof(*this), 1, fp);
// printf("%d",x);
//}
fseek(jPtr, (number-1)* sizeof(struct jobData), SEEK_SET);
if(isEmpty(*headPtr))
{
*headPtr = newPtr;
}
else
{
(*tailPtr)->nextPtr = newPtr;
}
*tailPtr = newPtr;
}
//else
//{
// / printf("Information not inserted. No memory available.\n");
// }
}
fclose(jPtr);
}