ftell() returns the position of the file stream. In this case (since you just opened the file), it will return zero. if(0 == NULL) is evaluating to true, so your program is exiting.
Printable View
ftell() returns the position of the file stream. In this case (since you just opened the file), it will return zero. if(0 == NULL) is evaluating to true, so your program is exiting.
Thank you. I used fseek() before ftell()
THis how I changed it , I decided to use do while instead of for loop, but it still not working, it loops though the loop, but I don't see any data that in the file to be stored into linked list
Code:#include<stdio.h>
#include<stdlib.h>
#include"keytype.h"
void load(KEYS k,char *fn)
{
KEYS curr;
FILE* fp;
k=(KEYS*)malloc(sizeof(KEYS));
curr=k;
if((fp=fopen(fn, "r"))== NULL)
{
printf("Error, the file does not exist");
exit(1);
}
fseek (fp, 0, SEEK_END);
if(ftell(fp)==0)
{
printf("IIIII\n");
exit(2);
}
do
{
curr->next=(KEYS*)malloc(sizeof(KEYS));
fscanf(fp, "%s", curr->next->addressee);
fscanf(fp, "%s", curr->next->sender);
fscanf(fp, "%s", curr->next->regarding);
fscanf(fp, "%d%*c%d%*c%d", &curr->next->date.month, &curr->next->date.day, &curr->next->date.year);
fscanf(fp, "%d", &curr->next->id);
fscanf(fp, "%s", curr->next->fname);
}while(!feof(fp));
curr->next=NULL;
fclose(fp);
/*
curr=k;
while(curr!=NULL)
{
printf( "%s\n", curr->addressee);
curr=curr->next;
}
*/
}
You seek to the end of the file, then try and read data from the file. There's nothing to read since you are at the end of the file. You need to rewind() to the beginning of the file.