Using Linked List to write to a file

Reply

Join Date: Apr 2009
Posts: 5
Reputation: natureG is an unknown quantity at this point 
Solved Threads: 0
natureG natureG is offline Offline
Newbie Poster

Using Linked List to write to a file

 
0
  #1
Apr 23rd, 2009
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.
  1. int main (void)
  2. {
  3. FILE *jPtr;
  4. JobPtr headPtr = NULL;
  5. JobPtr tailPtr = NULL;
  6. int choice;
  7. int number;
  8. char item;
  9.  
  10. main_menu();
  11.  
  12. while(choice!=4)
  13. {
  14. switch(choice)
  15. {
  16. case 1:
  17. system("cls");
  18. addFunction(&headPtr,&tailPtr);
  19. main_menu();
  20. scanf("%d", &choice);
  21. break;
  22.  
  23. case 2:
  24. system("cls");
  25. if(!isEmpty(headPtr))
  26. {
  27. item = removeFunction(&headPtr, &tailPtr);
  28. printf("Job has been removed from list.\n");
  29. }
  30. main_menu();
  31. scanf("%d", &choice);
  32. break;
  33.  
  34. case 3:
  35. system("cls");
  36. displayFunction(headPtr);
  37. main_menu();
  38. scanf("%d", &choice);
  39. break;
  40.  
  41. default:
  42. printf("Invalid choice.\n");
  43. system("cls");
  44. main_menu();
  45. scanf("%d", &choice);
  46. break;
  47. }
  48.  
  49. }
  50. return 0;
  51. }
  52.  
  53. /******************************************************************************/
  54. /* addFunction */
  55. /******************************************************************************/
  56.  
  57. void addFunction(JobPtr *headPtr, JobPtr *tailPtr)
  58. {
  59.  
  60. FILE *jPtr;
  61. int number;
  62. JobPtr newPtr; /*pointer to new node*/
  63. newPtr = malloc(sizeof(job)); /*create new node*/
  64. //struct jobData jobs = {0,"","","","","","",""};
  65. if ((jPtr = fopen("jobticket.txt", "w"))== NULL)
  66. {
  67. printf("File could not be opened\n");
  68. }
  69. fclose (jPtr);
  70. if ((jPtr = fopen("jobticket.txt", "rb+"))==NULL)
  71. {
  72. printf("File could not be opened\n");
  73. } /*end if*/
  74. else
  75. {
  76. while (fwrite(newPtr,1, sizeof(*newPtr),jPtr)>0)
  77. {
  78. printf("Enter jobnum: ");
  79. scanf("%d",&number);
  80. fflush(stdin);
  81. if (number == 0)
  82. break;
  83.  
  84.  
  85. fseek(jPtr, (number-1)* sizeof(struct jobData), SEEK_SET);
  86. fread(newPtr, sizeof(*newPtr),1,jPtr);
  87.  
  88. newPtr->jobnum = number;
  89.  
  90.  
  91. printf("Enter a brief description of problem: \n");
  92. gets(newPtr->jissue);
  93.  
  94.  
  95. printf("Enter worktype: \n");
  96. gets(newPtr->wtype);
  97.  
  98. printf("Enter date. Enter in this format DD/MM/YY: \n");
  99. gets(newPtr->date);
  100.  
  101. printf("Enter technician to which job will be assigned. "
  102. "(technician chosen according to work location): \n");
  103. gets(newPtr->techcode);
  104.  
  105. newPtr->nextPtr=NULL;
  106.  
  107.  
  108. fwrite(newPtr,1,sizeof(struct jobData),jPtr); //fwrite(this, sizeof(*this), 1, fp);
  109. // printf("%d",x);
  110. //}
  111. fseek(jPtr, (number-1)* sizeof(struct jobData), SEEK_SET);
  112.  
  113.  
  114. if(isEmpty(*headPtr))
  115. {
  116. *headPtr = newPtr;
  117. }
  118. else
  119. {
  120. (*tailPtr)->nextPtr = newPtr;
  121. }
  122. *tailPtr = newPtr;
  123. }
  124. //else
  125. //{
  126. // / printf("Information not inserted. No memory available.\n");
  127. // }
  128. }
  129. fclose(jPtr);
  130.  
  131. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Using Linked List to write to a file

 
0
  #2
Apr 23rd, 2009
Some points for now:
  1. newPtr = malloc(sizeof(job)); /*create new node*/
What's job? I don't see job anywhere.


  1. if ((jPtr = fopen("jobticket.txt", "w"))== NULL)
  2. {
  3. printf("File could not be opened\n");
  4. }
  5. 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
Last edited by Aia; Apr 23rd, 2009 at 11:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: natureG is an unknown quantity at this point 
Solved Threads: 0
natureG natureG is offline Offline
Newbie Poster

Re: Using Linked List to write to a file

 
0
  #3
Apr 24th, 2009
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.
  1. struct jobData
  2. {
  3. int jobnum;
  4. char itemname[20];
  5. char jstatus[10];
  6. char wtype[10];
  7. char date[10];
  8. char techcode[10];
  9. char jreport[225];
  10. char jissue[225];
  11. struct jobData *nextPtr;
  12. };
  13. typedef struct jobData job;
  14. typedef job *JobPtr;
Last edited by Ancient Dragon; Apr 24th, 2009 at 10:52 am. Reason: correct code tags [/code] not [\code]
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 5
Reputation: natureG is an unknown quantity at this point 
Solved Threads: 0
natureG natureG is offline Offline
Newbie Poster

Re: Using Linked List to write to a file

 
0
  #4
Apr 24th, 2009
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.
  1. struct jobData
  2. {
  3. int jobnum;
  4. char itemname[20];
  5. char jstatus[10];
  6. char wtype[10];
  7. char date[10];
  8. char techcode[10];
  9. char jreport[225];
  10. char jissue[225];
  11. struct jobData *nextPtr;
  12. };
  13. typedef struct jobData job;
  14. typedef job *JobPtr;

and the modified code

  1. /******************************************************************************/
  2. /* addFunction */
  3. /******************************************************************************/
  4.  
  5. void addFunction(JobPtr *headPtr, JobPtr *tailPtr)
  6. {
  7.  
  8. struct jobData job = {0,"","","","","","",""};
  9. FILE *jPtr;
  10. char input[3];
  11. int number;
  12. JobPtr newPtr; /*pointer to new node*/
  13. newPtr = malloc(sizeof(job)); /*create new node*/
  14.  
  15.  
  16. if ((jPtr = fopen("jobticket.txt", "rb+"))==NULL)
  17. {
  18. printf("File could not be opened\n");
  19. } /*end if*/
  20. else
  21. {
  22. while (fwrite(newPtr,1, sizeof(*newPtr),jPtr)>0)
  23. {
  24. fputs("Enter jobnum: ",stdout);
  25. fflush(stdout);
  26. if ( fgets(input, sizeof input, stdin) )
  27. {
  28. if ( sscanf(input, "%d", &number) == 1 )
  29. {
  30. if (number == 0)
  31. break;
  32.  
  33. }
  34.  
  35. }
  36.  
  37.  
  38.  
  39. fseek(jPtr, (number-1)* sizeof(struct jobData), SEEK_SET);
  40. fread(newPtr, sizeof(*newPtr),1,jPtr);
  41.  
  42. newPtr->jobnum = number;
  43.  
  44.  
  45. printf("Enter a brief description of problem: \n");
  46. mygetline(newPtr->jissue,sizeof newPtr->jissue);
  47.  
  48.  
  49. printf("Enter worktype: \n");
  50. mygetline(newPtr->wtype,sizeof newPtr->wtype);
  51.  
  52. printf("Enter date. Enter in this format DD/MM/YY: \n");
  53. mygetline(newPtr->date,sizeof newPtr->date);
  54.  
  55. printf("Enter technician to which job will be assigned. "
  56. "(technician chosen according to work location): \n");
  57. mygetline(newPtr->techcode,sizeof newPtr->techcode);
  58.  
  59. newPtr->nextPtr=NULL;
  60.  
  61.  
  62. fwrite(newPtr,1,sizeof(struct jobData),jPtr);
  63.  
  64. fseek(jPtr, (number-1)* sizeof(struct jobData), SEEK_SET);
  65.  
  66.  
  67. if(isEmpty(*headPtr))
  68. {
  69. *headPtr = newPtr;
  70. }
  71. else
  72. {
  73. (*tailPtr)->nextPtr = newPtr;
  74. }
  75. *tailPtr = newPtr;
  76. }
  77. //else
  78. //{
  79. // / printf("Information not inserted. No memory available.\n");
  80. // }
  81. }
  82. fclose(jPtr);
  83.  
  84. }

NB. I've learned so much from the tutorials on this website. Thank you. Keep up the good work.
Last edited by natureG; Apr 24th, 2009 at 7:14 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC