问题遇到的现象和发生背景
无法写入文件
问题相关代码,请勿粘贴截图
#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