| | |
Using Linked List to write to a file
![]() |
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
C Syntax (Toggle Plain Text)
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:
What's job? I don't see job anywhere.
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.
>
C Syntax (Toggle Plain Text)
newPtr = malloc(sizeof(job)); /*create new node*/
c Syntax (Toggle Plain Text)
if ((jPtr = fopen("jobticket.txt", "w"))== NULL) { printf("File could not be opened\n"); } fclose (jPtr);
>
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.
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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.
C Syntax (Toggle Plain Text)
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;
Last edited by Ancient Dragon; Apr 24th, 2009 at 10:52 am. Reason: correct code tags [/code] not [\code]
•
•
Join Date: Apr 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
and the modified code
NB. I've learned so much from the tutorials on this website. Thank you. Keep up the good work.
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.
C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
/******************************************************************************/ /* 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.
Last edited by natureG; Apr 24th, 2009 at 7:14 am.
![]() |
Similar Threads
- Linked List Problem (C++)
- Help with linked list in Turbo Pascal! (Pascal and Delphi)
- Double linked list - possible memory error (C)
- Linked List (insertion sort) (C)
- need help on linked list (C)
- Linked list (C++)
- sort linked list help please (C)
- Please Help With Linked List Programming (C++)
Other Threads in the C Forum
- Previous Thread: Const volatile
- Next Thread: Reading binary file without knowing file format
| Thread Tools | Search this Thread |
#include adobe ansi api array asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork forloop frequency function getlasterror givemetehcodez global grade graphics gtkgcurlcompiling hacking hardware highest histogram i/o include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue mysql number odf opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape socket socketprograming standard string systemcall threads turboc unix user voidmain() wab windows.h windowsapi






