Я хочу записать данные в структуре в текстовый файл. Но в итоге он показывает странные символы в текстовом файле. Я изменил многие формы аргументов fwrite, но ни одна из них не работает. Пожалуйста, кто-нибудь, помогите мне. Извините за длинный код
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student
{
char name[30];
int id;
int score;
int score2;
int score3;
};
int main()
{
printf("Please input the information below into grade.data!\n");
printf("Ends with name's value equal to 'E'\n");
struct Student stu[10];
int i = 0, maxlength = 0; //size of longest name
printf("Name No Math Chi Eng\n");
while (true){
scanf("%s", stu[i].name);
if(maxlength < strlen(stu[i].name)) maxlength = strlen(stu[i].name);
if (stu[i].name[0] == 'E') break;
scanf("%d", &stu[i].id);
scanf("%d", &stu[i].score1);
scanf("%d", &stu[i].score2);
scanf("%d", &stu[i].score3);
i++;
}
FILE* fp;
fp = fopen("test.txt", "wb");
if (fp == NULL) {
printf("Open file error!");
exit(-1);
}
fwrite(&stu, sizeof(struct Student), 1, fp);
fclose(fp);
printf("Name%-*c No%-*c Math Chi Eng\n", maxlength-4, ' ', 7, ' ');
for (int i = 0; stu[i].name[0] != 'E'; i++) {
printf("%-*s ", maxlength, stu[i].name);
printf("%-*d ", 9, stu[i].id);
printf("%-*d ", 4, stu[i].score1);
printf("%-*d ", 3, stu[i].score2);
printf("%d\n", stu[i].score3);
}
return 0;
}