Hello, I am working on function that builds the linked list of nodes, program exits when it comes to the line where file needs to be opened. don't understand why? To the function I am passing pointer to the new list and a file name. I was trying to find the answere myself.....with no success
I also have another question, when I am passing linked list to the function and when I need to loop through it or somthing else.....does it always start from a first node ...head or I need to tell the function from where in a linked list to sart to do whatever .....if yes, can I do it like k=NULL, and then k=k->next?
thank you in advance
Code:
#include<stdio.h>
#include"keytype.h"
void load(KEYS k,char *fn)
{
FILE* fp;
k=(KEYS*)malloc(sizeof(KEYS));
if((fp=fopen(fn, "r"))== NULL)
{
printf("Error, the file does not exist");
exit(1);
}
if(ftell(fp)==NULL)
exit(2);
fscanf(fp, "%s", k->addressee);
fscanf(fp, "%s", k->sender);
fscanf(fp, "%s", k->regarding);
fscanf(fp, "%d%*c%d%*c%d", &k->date.month, &k->date.day, &k->date.year);
fscanf(fp, "%d", &k->id);
fscanf(fp, "%s", k->fname);
while(!feof(fp))
{
k->next=(KEYS*)malloc(sizeof(KEYS));
k=k->next;
fscanf(fp, "%s", k->next->addressee);
fscanf(fp, "%s", k->next->sender);
fscanf(fp, "%s", k->next->regarding);
fscanf(fp, "%d%*c%d%*c%d", &k->next->date.month, &k->next->date.day, &k->next->date.year);
fscanf(fp, "%d", &k->next->id);
fscanf(fp, "%s", k->next->fname);
}
fclose(fp);
while(k->next!=NULL)
{
k=k->next;
printf("%s %s %d\n", k->next->addressee, k->next->sender, k->next->id);
}
}
Thank you.