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

时间:2016-05-19 02:43:33

标签: c file-io fwrite

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

fp=fopen("pre-survey.txt", "a");
printf("name\n");
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;

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您可以使用fprintf功能。

类似的东西:

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