Thread: Linked List to file

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    Linked List to file

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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are using fwrite, which writes binary data - if you want to write textual output, you need to use (something like) fprintf() and fscanf().

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Don't mix block I/O with linked lists.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by itCbitC View Post
    Don't mix block I/O with linked lists.
    It doesn't really have anything to do with linked lists. You don't want to be using fseek on files which you consider text files. It probably won't work right if you've got newlines in your file. You can use fread fine if you've used fwrite to write your structure. You just ignore the pointer spot after you've read it, and assign it to something new in memory.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM