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

标签 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;

有什么建议吗?

最佳答案

您可以使用 fprintf 函数。

类似于:

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

关于c - 在 C 中使用 fwrite 将存储在结构中的整数写入 .txt 文件,但是当我打开 .txt 文件时数字显示为框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37313087/

相关文章:

c - 段错误 fgets() 两遍汇编器

无法将PCM数据写入wav文件

c++ - 将文件拆分为相等的字节部分,由完整的字分隔 (C/C++)

java - 如何将逗号分隔的 ArrayList 添加到树中

php - fwrite 返回(资源暂时不可用)

c - 使用位置无关代码而不使用 GOT

c++ - Openssl,无效参数 ' Candidates are: int BN_set_word(bignum_st *, ?) '

jquery - 我如何知道我的输入类型 ="file"是否选择了内容

c - 将数据写入文本或二进制的单个 C 函数

c - fwrite() 如果任何单个字节的符号位为 1,则将全 1 写入更高有效位(写入 int)