Can anyone fix my problem? I'm sure my getDate function algorithm was right, but it still wrong..
here is the code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct{int day,month,year;}Date;
typedef struct{
char name[31],gender;
Date dob;
}Student;
void getString(FILE* x,char* y);
Date getDate(FILE* x);
char readChar(FILE *x);
int main(){
Student pupil[100];
int a,n=0;
char temp[31];
FILE* in=fopen("input.txt","rt");
getString(in,temp);
while(strcmp(temp,"END")!=0){
strcpy(pupil[n].name,temp);
pupil[n].dob=getDate(in);
pupil[n].gender=readChar(in);
n++;
getString(in,temp);
}
fclose(in);
for(a=0;a<3;a++){
printf("%s %d %d %d %c\n",pupil[a].name,pupil[a].dob.day,pupil[a].dob.month,pupil[a].dob.year,pupil[a].gender);
}
}
void getString(FILE *x,char* y){
char str[47];
char* strtemp;
fgets(str,47,x);
strtemp=strtok(str,"\"");
strcpy(y,strtemp);
fseek(x,-((int)sizeof(int)),SEEK_CUR);
}
Date getDate(FILE* x){
static Date temporary;
int cases;
static int number[3];
static char words[47];
static char* temp;
fgets(words,47,x);
temp=strtok(words,"\"");
for(cases=0;cases<3;cases++){
temp=strtok(NULL," ");
number[cases]=atoi(temp);
}
temporary.day=number[0];
temporary.month=number[1];
temporary.year=number[2];
return temporary;
}
char readChar(FILE *x){
int a;
static char gender,temp[47];
fseek(x,-((int)sizeof(int)),SEEK_CUR);
fgets(temp,47,x);
for(a=0;temp[a];a++){
if(temp[a]=='\n')gender=temp[a-1];
}
return gender;
}
"input.txt" contains:
"Puspita Mahdy Hanifa" 6 12 1999 F
"Royhan Mufid Akbar" 8 3 2003 M
"Tsabitah Naomi" 7 9 2009 F
END