Hello,
I am having an issue writing to a file using a linked list. I keep getting garbage being written to the .txt file and as such the 'display' function does not work. Any assistance would be appreciated. I've only included the add function but will gladly supply you with any additional information needed.
Code:
/******************************************************************************/
/* addFunction */
/******************************************************************************/
void addFunction(JobPtr *headPtr, JobPtr *tailPtr)
{
struct jobData job = {0,"","","","","","",""};
FILE *jPtr;
char input[3];
int number;
JobPtr newPtr; /*pointer to new node*/
newPtr = malloc(sizeof(job)); /*create new node*/
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)
{
fputs("Enter jobnum: ",stdout);
fflush(stdout);
if ( fgets(input, sizeof input, stdin) )
{
if ( sscanf(input, "%d", &number) == 1 )
{
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");
mygetline(newPtr->jissue,sizeof newPtr->jissue);
printf("Enter worktype: \n");
mygetline(newPtr->wtype,sizeof newPtr->wtype);
printf("Enter date. Enter in this format DD/MM/YY: \n");
mygetline(newPtr->date,sizeof newPtr->date);
printf("Enter technician to which job will be assigned. "
"(technician chosen according to work location): \n");
mygetline(newPtr->techcode,sizeof newPtr->techcode);
newPtr->nextPtr=NULL;
fwrite(newPtr,1,sizeof(struct jobData),jPtr); //fwrite(this, sizeof(*this), 1, fp);
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);
}