Search code examples
cfile-iofwrite

using fwrite in C to write integers stored in a structure into .txt file, but the numbers shows up as box when I open the .txt file


I am using the fwrite to write integers stored in a structure into a .txt file. However, when I open the file, the numbers are not readable (become box): Here is my code:

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 is a structure i declared:

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

Any advice?


Solution

  • You can use fprintf function.

    Something like:

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