我也遇到过这种乱码的问题,我后来改了一下,就可以了,我把那一块的函数给你贴上
int input_stu_xinxi()
{ /*************
int flag = 1;
while (flag)
{
struct stu students;
FILE *fp;
errno_t err= (fopen_s(&fp, "F:\stuinfo.txt", "w"));
if (err!= 0)
{
printf("文件打开错误\n");
exit(0);
}
if (fwrite(&students, sizeof(struct stu), 1, fp)!=1)
{
printf("\n file write error \n");
}
文件的写还需要完善
printf("\t学号:");
scanf("%s", students.stu_no);
fputs(students.stu_no,fp);
printf("\t姓名:");
scanf("%s", students.name);
fputs(students.name, fp);
printf("\t年龄:");
scanf("%s", students.age);
fputs(students.age, fp);
printf("\t性别:");
scanf("%s", students.sex);
fputs(students.sex, fp);
printf("\t出生年月:");
scanf("%s", students.birth_date);
fputs(students.birth_date, fp);
printf("\t地址:");
scanf("%s", students.address);
fputs(students.address, fp);
printf("\t电话:");
scanf("%s", students.phone);
fputs(students.phone, fp);
printf("\t邮件:");
scanf("%s", students.email);
fputs(students.email, fp);
printf("\n");
fclose(fp);
memset(&students,0x00,sizeof(struct stu));
printf("继续输入请按1,返回上级菜单请按2,退出请安0\n");
scanf_s("%d", &flag);
if (flag == 0)
{
exit(0);
}
if (flag == 1)
{
continue;
}
if (flag == 2)
{
menu();
break;
}
}
return 0;
******/
FILE *fp;
int i, m, flag = 1;
printf("请输入等级学生信息的总人数:");
scanf_s("%d", &m);
while (flag)
{
fp = fopen("F:\stuinfo.txt", "w");
//errno_t err = (fopen_s(&fp, "F:\stuinfo.txt", "w"));
if (fp == 0)
{
printf("文件打开错误\n");
exit(0);
}
/****
if (fwrite(&students[i], sizeof(struct stu), 1, fp) != 1)
{
printf("\n file write error \n");s
}
****/
for (i = 0; i < m; i++)
{
printf("学号:");
scanf("%s", &students[i].stu_no);
fputs(students[i].stu_no, fp);
fputs("\t", fp);
printf("姓名:");
scanf("%s", &students[i].name);
fputs(students[i].name, fp);
fputs("\t", fp);
printf("年龄:");
scanf("%s", &students[i].age);
fputs(students[i].age, fp);
fputs("\t", fp);
printf("性别:");
scanf("%s", &students[i].sex);
fputs(students[i].sex, fp);
fputs("\t", fp);
printf("出生年月:");
scanf("%s", &students[i].birth_date);
fputs(students[i].birth_date, fp);
fputs("\t", fp);
printf("地址:");
scanf("%s", &students[i].address);
fputs(students[i].address, fp);
fputs("\t", fp);
printf("电话:");
scanf("%s", &students[i].phone);
fputs(students[i].phone, fp);
fputs("\t", fp);
printf("邮件:");
scanf("%s", &students[i].email);
fputs(students[i].email, fp);
fputs("\n", fp);
}
fclose(fp);
printf("继续输入请按1,返回上级菜单请按2,退出请安0\n");
scanf_s("%d", &flag);
if (flag == 0)
{
exit(0);
}
if (flag == 1)
{
continue;
}
if (flag == 2)
{
menu();
break;
}
}
return 0;
}