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

Recommended Answers

All 3 Replies

Can anyone fix my problem?

No, that's your job. We can point out where you went wrong...

I'm sure my getDate function algorithm was right, but it still wrong..

... if you'd bother to tell us how you know it's wrong. With the info you gave we don't know what we're looking for.

Step through the code with a debugger, and watch the values of the variables, as they change. Use asserts and print statements as needed, to help you debug it.

This is not a "shades of grey" thing. If your function is wrong, then find the bug. Troubleshooting code is an essential skill for a programming student to develop. Nobody knows your code, better than you do.

fseek(x,-((int)sizeof(int)),SEEK_CUR);

That line looks suspect to me. Why are you backtracking the size of an int?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.