为什么我这个代码无法写入文件里面?

问题描述:

问题遇到的现象和发生背景

无法写入文件

问题相关代码,请勿粘贴截图
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 100

struct data
{
    char year;
    char month;
    char day;
};



struct stud
{
    struct data time;
    char whichsection;
    char lesson;
    char name[M];
    char type;
};
struct stud arrange[M];

FILE *fp;
int n = 0;
void menu();
void function1();
/*void function2();
void function3();
void function4();
void function5();
void function6();
void function7();*/


void main()
{
    menu();
}
/***********************************************menu()***********************************************************/
void menu()
{
    int choice;
    printf("------------------------------------------------------------------------------------------------\n");
    printf("||~~~***The student attendance information***~~~||\n");
    printf("================================================================================================\n");
    printf("||1.input       the information                                       2.browse the imformation||\n");
    printf("||3.aad         the imformation                                       4.delete the infoemation||\n");
    printf("||5.search      the information                                       6.amendant  the   record||\n");
    printf("||7.statistical the information                                       0.exit      the   system||\n");
    printf("================================================================================================\n");
    printf("| The ueser can make a choice form the funtion above |\n");
    printf("------------------------------------------------------------------------------------------------\n");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1:function1(); break;
        /*case'2':function2(); break;
        case'3':function3(); break;
        case'4':function4(); break;
        case'5':function5(); break;
        case'6':function6(); break;
        case'7':function7(); break;*/
    case 0:exit(0);
    }
    menu();

}
/**********************************************input the imformation***********************************************/
void function1()
{
    int a, n;
    if ((fp = fopen("E:\\data\\info.txt", "w")) == NULL)
    {
        printf("can't open the file");
        exit(1);
    }

    for (n = 1; n <= M; n++)
    {
        printf("input the data:");
        scanf("%s %s %s", &arrange[n].time.year, &arrange[n].time.month, &arrange[n].time.day);

        printf("input what the student name is:");
        scanf("%s", &arrange[n].name[100]);

        printf("input which section:");
        scanf("%s", &arrange[n].whichsection);
        
        printf("input what the lesson is:");
        scanf("%s", &arrange[n].lesson);

        printf("input what the type is:");
        scanf("%s", &arrange[n].type);

        printf("If you want to input more,press 1 ,or press 0\n");
        scanf("%d", &a);
        if (a == 0)
            break;
        if (a == 1)
            printf("\n");
    }
    if (fread(&arrange[n], sizeof(struct stud), n, fp) != n)
    {
        printf("you can't write the file\n");
        exit(1);
    }
    fclose(fp);
}
    

运行结果及报错内容

you can't write the file

我的解答思路和尝试过的方法
我想要达到的结果

修改如下,见注释,供参考:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define M 100
struct data
{
    char year[5]; //修改
    char month[3];//修改
    char day[3];  //修改
};
struct stud
{
    struct data time;
    char whichsection[16];//修改
    char lesson[16];      //修改
    char name[M];         //修改
    char type[16];        //修改
};
struct stud arrange[M];
FILE* fp;
int n = 0;
void menu();
void function1();
/*void function2();
void function3();
void function4();
void function5();
void function6();
void function7();*/

void main()
{
    menu();
}
/***********************************************menu()***********************************************************/
void menu()
{
    int choice;
    printf("------------------------------------------------------------------------------------------------\n");
    printf("                       ||~~~***The student attendance information***~~~||                       \n");//修改
    printf("================================================================================================\n");
    printf("||1.input       the information                                       2.browse the imformation||\n");
    printf("||3.aad         the imformation                                       4.delete the infoemation||\n");
    printf("||5.search      the information                                       6.amendant  the   record||\n");
    printf("||7.statistical the information                                       0.exit      the   system||\n");
    printf("================================================================================================\n");
    printf("| The ueser can make a choice form the funtion above |\n");
    printf("------------------------------------------------------------------------------------------------\n");
    scanf("%d", &choice);
    switch (choice)
    {
    case 1:function1(); break;
        /*case'2':function2(); break;
        case'3':function3(); break;
        case'4':function4(); break;
        case'5':function5(); break;
        case'6':function6(); break;
        case'7':function7(); break;*/
    case 0:exit(0);
    }
    menu();
}
/**********************************************input the imformation***********************************************/
void function1()
{
    int a = 1;   // n; //修改
    if ((fp = fopen("E:\\data\\info.txt", "w")) == NULL)
    {
        printf("can't open the file");
        exit(1);
    }
    for (n = 0; n < M && a != 0; n++) //for (n = 1; n <= M; n++) //修改
    {
        printf("input the data:");
        scanf("%s %s %s", arrange[n].time.year, arrange[n].time.month, arrange[n].time.day); //修改
        //canf("%s %s %s", &arrange[n].time.year, &arrange[n].time.month, &arrange[n].time.day);
        printf("input what the student name is:");
        scanf("%s", arrange[n].name);         //修改
        //scanf("%s", &arrange[n].name[100]);
        printf("input which section:");
        scanf("%s", arrange[n].whichsection);  //修改 
        //scanf("%s", &arrange[n].whichsection);
        printf("input what the lesson is:");
        scanf("%s", arrange[n].lesson);       //修改
        //scanf("%s", &arrange[n].lesson);
        printf("input what the type is:");
        scanf("%s", arrange[n].type);         //修改
        //scanf("%s", &arrange[n].type);
        printf("If you want to input more,press 1 ,or press 0\n");
        scanf("%d", &a);
        //if (a == 0)     //修改
        //    break;
        //if (a == 1)
        //    printf("\n");
    }
    if (fwrite(arrange, sizeof(struct stud), n, fp) != n)
        //fread(&arrange[n], sizeof(struct stud), n, fp) != n
    {
        printf("you can't write the file\n");
        exit(1);
    }
    fclose(fp);
}