You reading the file in binary format.
How did you create it ?
I took your code and deleted from your struct the integers.
with the file looking like this :
Dan ben
aaa bbb
hellow world
Dufi duck
It worked fine for me.
Code:
#define SEAT_NUM 12
#define MAX 20 /*no one has more than 20 characters in their names */
#define CHUNK 1 /*size of chunk to copy to file */
#define CLIENTS "Clients.dat"/*client file for CAF */
#define TAKEN 1
#define DELETE 2
#define AVAILABLE 0
#define FLUSH (while (getchar() !='\n') )
typedef struct pass{
/* int SeatNum; */
/* int SeatStatus; */
char LastName[MAX];
char FirstName[MAX];
struct pass* next_node;
}CAF;
const size_t SIZE = sizeof(CAF);
CAF *DataInit();/*intializes data from file saves to structs*/
int main(void)
{
CAF *head;
CAF *tmp;
if ( head = DataInit() )
for ( tmp = head ; tmp ; tmp = tmp->next_node)
printf("last = %s first = %s\n", tmp->LastName, tmp->FirstName );
getchar();
return 0;
}
CAF *DataInit()
{
FILE *fp;/*keeps addy of file looking for*/
int ndx=0, seatNum,seatStatus;
char firstName[MAX],lastName[MAX];
CAF *pass,*prev,*head=NULL;
/*attempts to open clients file and intialise data to Link List of CAF objects*/
if ( (fp = fopen(CLIENTS,"rb") )== NULL)
{
printf("%s was not available creating new file\n\n",CLIENTS);
if( (fp=fopen(CLIENTS,"ab") )==NULL)
perror("\aERROR");
}
/*go to biginning of file*/
puts("Passenger file successfully opened.");
rewind(fp);
/*while there is actual data to be initialized*/
/* while ( (fscanf(fp,"%d %d %s %s",&seatNum,&seatStatus,firstName,lastName)) == 4) */
while ( (fscanf(fp,"%s %s",firstName,lastName)) == 2)
{
/*malloc struct if no space end program*/
if ( ( pass = malloc(SIZE) )==NULL)
{
perror("alert:");
exit(EXIT_FAILURE);
}
/*else assign values to current passenger*/
/* pass->SeatNum=seatNum; */
/* pass->SeatStatus = seatStatus; */
strcpy(pass->FirstName,firstName);
strcpy(pass->LastName,lastName);
pass->next_node = NULL; /*close it off*/
/*place struct in CAF list object*/
if ( (head == NULL) )/*if its the first item in list head gets its value*/
head = pass;/*head points to the first item in the list which is an object*/
else
/*logic if its the second time, prev will already contain the addy of the first
structure - assigned in the last statement, so now link the next struct and
that will be what next_node will contain the addy of the next passenger (pass).
on each successive iteration. next node will always contain the addy of the next
object somewhere in memory.*/
prev->next_node = pass;/*where pass is the current passenger object*/
/*next iteration passenger will actually be the previous so assign to prev*/
prev = pass;/*so pass now in memory somewhere but no identifier but in mem*/
printf("client %d initialized\n",ndx++);
}
/*end of while function close the file and exit*/
if( (fclose(fp)) !=0)
perror("\aERROR===");
return head;
}/*end of function*/
Remarks:
a. If you fail to open the file in "rb" mode, what is the point opening it with mode "ab" ?
b. If you just opened the file why do you use rewind ?