Reading Data file into Linked List

OP (Company: Personal) #2752796
Please i am new to c,i intend to load content of a file into a linked 
list and display the list, please help debug the code, thank you.

 /* a linked list. */

 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>

 /* The list data structure. */
 struct data {
 char transtype[20];
 char transcount[3];
 char unitcost[3];


 struct data *next;

 };

 /* Define typedefs for the structure */
 /* and a pointer to it. */
 typedef struct data PERSON;
 typedef PERSON *LINK;

 int i;
 char namevar[20];
 FILE *fp;


 int main( void )
 {
 /* Head, new, and current element pointers. */
 LINK head = NULL;
 LINK new = NULL;
 LINK current = NULL;


 if ( (fp = fopen("filename", "rb")) == NULL)
     {
     //fprintf(stderr, “Error opening file %s.”, filename);
     exit(1);
     }

 //current = head;
  while (!feof(fp))
      {

       fread(transtype, sizeof(char), 20, fp);
       fread(transcount, sizeof(char),3, fp);
       fread(unitcost, sizeof(char), 3 , fp);


     //  current = current->next;
      }
       fclose(fp);

// Display List
       printf("\n%s", Ttranstype);
       printf("\n%s", Ttranscount);
                   printf("\n%s", Tunitcost);

 return 0;
 }

Reply

A question of your own? This thread is over 14 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.