使用C中的fwrite将存储在结构中的整数写入.txt文件,但当我打开.txt文件时,数字显示为框



我正在使用fwrite将存储在结构中的整数写入.txt文件。但是,当我打开文件时,数字是不可读的(变成方框):这是我的代码:

fp=fopen("pre-survey.txt", "a");
printf("namen");
scanf("%s", &temp.name);
printf("q1:n");
scanf("%d", &temp.q1);
printf("q2:n");
scanf("%d", &temp.q2);
printf("q3:n");
scanf("%d", &temp. q3);
fwrite(&temp, sizeof(temp), 1, fp);
fclose(fp);
}

temp是我声明的一个结构:

struct pre_survey{ 
char name[20]; int q1; int q2; int q3; };
struct pre_survey temp; 
struct pre_survey get;

有什么建议吗?

您可以使用fprintf函数。

类似于:

fprintf(fp, "%s, %d, %d, %d", temp.name, temp.q1, temp.q2, temp.q3);