Hỏi về cách truyền ngày tháng năm vào file

Sau khi mình thêm hàm isValid để check giá trị vào thì dữ liệu của mình không được nhập vào file studen4.txt nữa. Tại sao và cách fix như thế nào ạ. Xin cảm ơn!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VALUE 100
int d, m, y;

struct student {
    char code[MAX_VALUE];
    char name[MAX_VALUE];
    char dateOfBirth[MAX_VALUE];
    float point;
};

int ReadFile(struct student studentRead[], int *studentTotal) {
    FILE *f = fopen("Student4.txt", "r");
    int i = 0;
    if (f != NULL) {
        while (!feof(f)) {
            fscanf(f, "%[^\n] %[^\n] %[^\n] %f%*c", studentRead[i].code, studentRead[i].name, studentRead[i].dateOfBirth, &studentRead[i].point);
            i += 1;
            *studentTotal += 1;
        }
    }
    *studentTotal -= 1;
}

int WriteFile(struct student studentStore) {
    FILE *f = fopen("Student4.txt", "a");
    if (f != NULL) {
        fprintf(f, "%s\n", studentStore.code);
        fprintf(f, "%s\n", studentStore.name);
        fprintf(f, "%s\n", studentStore.dateOfBirth);
        fprintf(f, "%f\n", studentStore.point);
    }
    fclose(f);

}

int sortByName() {
    int studentTotal, i, j;
    struct student studentStoreRead[100];
    ReadFile(studentStoreRead, &studentTotal);
    struct student temp;
    for (i = 0; i < studentTotal - 1; i++) {
        for (j = i + 1; j < studentTotal; j++) {
            if (strcmp(studentStoreRead[i].name, studentStoreRead[j].name) > 0) {
                temp = studentStoreRead[i];
                studentStoreRead[i] = studentStoreRead[j];
                studentStoreRead[j] = temp;
            }
        }
    }
    FILE *f = fopen("Student4.txt", "w");
    if (f != NULL) {
        for (i = 0; i < studentTotal; i++) {
            fprintf(f, "%s\n", studentStoreRead[i].code);
            fprintf(f, "%s\n", studentStoreRead[i].name);
            fprintf(f, "%s\n", studentStoreRead[i].dateOfBirth);
            fprintf(f, "%f\n", studentStoreRead[i].point);
        }
    }
    fclose(f);
}

bool isValid(int d, int m, int y) {
    if (d >= 1 && m >= 1 && m <= 12) {
        if ((y % 400 == 0 || (y % 4 == 0 && y % 100 != 0) && m == 2 && d <= 29)) {
            return true;
        } else {
            int maxd[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            return d <= maxd[m - 1];
        }
    } else return false;
}

int enterStudent(struct student studentStore, int *studentTotal) {
    char ch;
    int m;
    m = isValid(d, m, y);
    struct student studentStoreRead[100];
    int flag = 0, i;
    do {
        printf("code: ");

        do {
            flag = 0;
            scanf("%s%*c", &studentStore.code);
            strupr(studentStore.code);
            ReadFile(studentStoreRead, studentTotal);
            for (i = 0; i<*studentTotal; i++) {
                if (strcmp(studentStore.code, studentStoreRead[i].code) == 0) {
                    flag = 1;
                    printf("The code is not available!!!\nInput again: ");
                }
            }
        } while (flag == 1);
        printf("name: ");
        gets(studentStore.name);
        fflush(stdin);
        int m = isValid(d, m, y);
        do {
            printf("date of birth: ");
            scanf("%d/%d/%d%*c", &studentStore.dateOfBirth);
            if (d < 1 || m < 1 || y < 0) {
                printf("loi roi nha");
            } else isValid(d, m, y);
        } while (m == true);





        printf("point: ");
        scanf("%f%*c", &studentStore.point);
        WriteFile(studentStore);
        sortByName();
        printf("Press enter to continue, ESC to return the main menu \n");
        fflush(stdin);
        ch = getchar();
    } while (ch != 27);
}

int display(struct student studentStoreRead[], int index) {
    printf("--------------------------\n");
    printf("code: %s\n", studentStoreRead[index].code);
    printf("name: %s\n", studentStoreRead[index].name);
    printf("day of birth: %s\n", studentStoreRead[index].dateOfBirth);
    printf("point: %.2f\n", studentStoreRead[index].point);
    printf("--------------------------\n");
}

int displayAll(int *studentTotal) {
    int i;
    struct student studentStoreRead[100];
    ReadFile(studentStoreRead, studentTotal);
    for (i = 0; i<*studentTotal; i++) {
        display(studentStoreRead, i);
    }
}

int lookUp(int *studentTotal) {
    struct student studentStoreRead[100];
    struct student studentCheck;
    while (getchar() != '\n');
    ReadFile(studentStoreRead, studentTotal);
    printf("Enter name to look up: ");
    gets(studentCheck.name);
    int flag = 0, i;
    for (i = 0; i<*studentTotal && flag == 0; i++) {
        if (strstr(studentStoreRead[i].name, studentCheck.name) > 0) {
            display(studentStoreRead, i);
        }
    }
    if (flag == 0) {
        printf("Student doesn't exist \n");
        return 0;
    } else {
        return 1;
    }
}

int menu() {
    printf("1.Enter student list\n");
    printf("2.Look up student\n");
    printf("3.Display student list\n");
    printf("4.Exit\n");
}

int main() {
    int choice;
    int m;
    do {
        menu();
        printf("Enter your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
            {

                struct student studentStore;
                int studentTotal = 0;
                enterStudent(studentStore, &studentTotal);

                break;
            }
            case 2:
            {
                int studentTotal = 0;
                lookUp(&studentTotal);
                break;
            }
            case 3:
            {
                int studentTotal = 0;
                displayAll(&studentTotal);
                break;
            }
            case 4:
            {
                break;
            }
        }
    } while (choice != 4);
}

“Chôm” hàm isValid() bên C++ phải không? Trong hàm enterStudent()scanf() sai và else vô nghĩa.

3 Likes

vậy để truyên ngày tháng năm vào file ta làm như thế nào vậy ạ

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?