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);

}

Some points for now:

newPtr = malloc(sizeof(job));       /*create new node*/

What's job? I don't see job anywhere.

if ((jPtr = fopen("jobticket.txt", "w"))== NULL)
   {
      printf("File could not be opened\n");
   }
   fclose (jPtr);

Doesn't make any sense regardless the file gets opened or not you close it right away. You can not close a file that's not opened.

> gets(newPtr->jissue); Forget you know there's a function named gets(). It is not good to use in any circumstance, because it will always overflow the buffer given the opportunity. fgets() is a very appropriated alternative

Thank you for your speedy reply. job is a variable of the struct jobData.
Sorry about the file section. It should have been commented out.
Thank you for the advice on fgets, I will research and start implementing it. I'll let you know what happens. Thanks again for your speedy reply.

struct jobData 
{
   int jobnum;
   char itemname[20];
   char jstatus[10];
   char wtype[10];
   char date[10];
   char techcode[10];
   char jreport[225];
   char jissue[225];
   struct jobData *nextPtr;
};
typedef struct jobData job;
typedef job *JobPtr;

Thank you again for your speedy reply. I realized that I had 'jobs' instead of job and made that change.

Thank you for the advice on fgets. I did some research and removed the 'gets' and 'scanf' statements.

Garbage is still being printed to the .txt file. However, it's 'improved garbage', (not sure if that makes sense but I can see more information than last time).
Thanks again for any assistance you can give.

I've included the struct.

struct jobData 
{
   int jobnum;
   char itemname[20];
   char jstatus[10];
   char wtype[10];
   char date[10];
   char techcode[10];
   char jreport[225];
   char jissue[225];
   struct jobData *nextPtr;
};
typedef struct jobData job;
typedef job *JobPtr;

and the modified 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); 

          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);

}

NB. I've learned so much from the tutorials on this website. Thank you. Keep up the good work.

This article has been dead for over six months. Start a new discussion instead.