/*
Rudimentary StudentRecord system in C ... R.Metcalfe 7/4/2013
File Based: nextsid.is for next student id
            [student_id].rec has a student record
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <time.h>
#include <math.h>

struct mystruct {
    long student_number;
    char name[101];
    char address_1[201];
    char address_2[201];
    char suburb[101];
    char city[101];
    char state[51];
    char country[101];
    char postcode[51];
    char email[101];
    char telephone[51];
    char mobile[51];
    float mark;
};

long get_next_student_number() {
    FILE *mf;
    long sid = 1;
    mf = fopen("nextsid.is", "r");
    if (mf == NULL) {
        mf = fopen("nextsid.is", "w");
        fprintf(mf, "%d", 1);
        fclose(mf);
        return 1;
    } else if (fscanf(mf, "%ld", &sid)) {
        fclose(mf);
        sid++;
        mf = fopen("nextsid.is", "w");
        fprintf(mf, "%ld", sid);
        fclose(mf);
    }
    return sid;
}

int display_record(struct mystruct msy) {
    int reenter = 0;
    char ans[6];
    printf("\n\nStudent Number %ld (please note this down)\nName %s\nAddress1 %s\nAddress2 %s\nSuburb %s\nCity %s\nState %s\nCountry %s\nPostcode %s\nEmail %s\nTelephone %s\nMobile %s\nMark %.2f\n\nType 1 to reenter data, else it will be accepted: ",
           msy.student_number, msy.name, msy.address_1, msy.address_2, msy.suburb,
           msy.city, msy.state, msy.country, msy.postcode, msy.email, msy.telephone,
           msy.mobile, msy.mark);
    fgets(ans, 5, stdin);
    if (ans[0] == '1') reenter = 1;
    return reenter;
}

void write_record(struct mystruct *msyp) {
    FILE *mf;
    char fnam[101];
    sprintf(fnam, "%ld.rec", msyp->student_number);
    mf = fopen(fnam, "w");
    if (strstr(msyp->name, "\n")) (*strstr(msyp->name, "\n")) = 0;
    if (strstr(msyp->address_1, "\n")) (*strstr(msyp->address_1, "\n")) = 0;
    if (strstr(msyp->address_2, "\n")) (*strstr(msyp->address_2, "\n")) = 0;
    if (strstr(msyp->suburb, "\n")) (*strstr(msyp->suburb, "\n")) = 0;
    if (strstr(msyp->city, "\n")) (*strstr(msyp->city, "\n")) = 0;
    if (strstr(msyp->state, "\n")) (*strstr(msyp->state, "\n")) = 0;
    if (strstr(msyp->country, "\n")) (*strstr(msyp->country, "\n")) = 0;
    if (strstr(msyp->postcode, "\n")) (*strstr(msyp->postcode, "\n")) = 0;
    if (strstr(msyp->email, "\n")) (*strstr(msyp->email, "\n")) = 0;
    if (strstr(msyp->telephone, "\n")) (*strstr(msyp->telephone, "\n")) = 0;
    if (strstr(msyp->mobile, "\n")) (*strstr(msyp->mobile, "\n")) = 0;
    fprintf(mf, " %ld| %s| %s| %s| %s| %s| %s| %s| %s| %s| %s| %s| %f\n", msyp->student_number,
            msyp->name, msyp->address_1, msyp->address_2, msyp->suburb, msyp->city, msyp->state,
            msyp->country, msyp->postcode, msyp->email, msyp->telephone, msyp->mobile,
            msyp->mark);
    fclose(mf);
}

void read_record(struct mystruct *msyp, FILE *mf) {
    char arec[501], *ok, *tok;
    long along;
    float afloat;
    if ((ok = fgets(arec, 500, mf))) {
        if (strstr(arec, "\n")) (*strstr(arec, "\n")) = 0;
        tok = strtok(arec, "|");
        if (tok != NULL) {
            sscanf(tok, "%ld", &along);
            msyp->student_number = along;
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->name, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->address_1, tok);
            tok = strtok(NULL, "|");
            if (strlen(tok)) strcpy(msyp->address_2, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->suburb, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->city, tok);
            tok = strtok(NULL, "|");
            if (strlen(tok)) strcpy(msyp->state, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->country, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->postcode, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->email, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->telephone, tok);
            tok = strtok(NULL, "|");
            if (tok && strlen(tok)) strcpy(msyp->mobile, tok);
            tok = strtok(NULL, "|");
            if (tok && strstr(tok, "\n")) *(strstr(tok, "\n")) = 0;
            if (tok) sscanf(tok, "%f", &afloat);
            if (tok) msyp->mark = afloat;
        }
    }
    fclose(mf);
}

int main()
{
    FILE *myf = NULL;
    char fnm[101], *fg, answer[51];
    struct mystruct msys;
    strcpy(msys.name, " ");
    while (strlen(msys.name) > 0) {
          if (strlen(msys.name) > 0) {
            if (myf == NULL) {
                 printf("Please enter\n\nName (entering filename (ie. student_id + .rec will read in a record) ({cr} exits): ");
                 fg = fgets(msys.name, 100, stdin);
                 if (strstr(msys.name, "\n")) *(strstr(msys.name, "\n")) = 0;
                 if (strlen(msys.name) > 0) {
                  myf = fopen(msys.name, "r");
                  if (myf == NULL) msys.student_number = get_next_student_number();
                 }
            } else {
                 fclose(myf);
                 myf = NULL;
            }
            if (myf != NULL) {
                 read_record(&msys, myf);
                 myf = NULL;
            } else if (strlen(msys.name) > 0) {
                 printf("Address1: ");
                 fg = fgets(msys.address_1, 200, stdin);
                 printf("Address2: ");
                 fg = fgets(msys.address_2, 200, stdin);
                 printf("Suburb: ");
                 fg = fgets(msys.suburb, 100, stdin);
                 printf("City: ");
                 fg = fgets(msys.city, 100, stdin);
                 printf("State: ");
                 fg = fgets(msys.state, 50, stdin);
                 printf("Country: ");
                 fg = fgets(msys.country, 100, stdin);
                 printf("Postcode: ");
                 fg = fgets(msys.postcode, 50, stdin);
                 printf("Email: ");
                 fg = fgets(msys.email, 100, stdin);
                 printf("Telephone: ");
                 fg = fgets(msys.telephone, 50, stdin);
                 printf("Mobile: ");
                 fg = fgets(msys.mobile, 50, stdin);
                 printf("Student Mark: ");
                 fg = fgets(answer, 50, stdin);
                 if (strstr(answer, "\n")) *(strstr(answer, "\n")) = 0;
                 sscanf(answer, "%f", &msys.mark);
                 write_record(&msys);
                 myf = NULL;
            }
            if (strlen(msys.name) > 0) {
             if (display_record(msys) == 1) {
                sprintf(fnm, "%ld.rec", msys.student_number);
                myf = fopen(fnm, "r");
                printf("Please enter\n\nName for student_id=%ld: ", msys.student_number);
                fg = fgets(msys.name, 100, stdin); 
                if (strstr(msys.name, "\n")) *(strstr(msys.name, "\n")) = 0;
             }
            }
         }

    }
    
    return 0;
}
