I am trying to write some records (patient info) into a .dat file using C. The patient info contains the National ID no., name, gender and the telephone no.
Here's my code:
#include<stdio.h>
int main(void)
{
char order = 'y';
char nic[12];
char name[10];
char gender = ' ';
char tel[10];
FILE *fp;
fp = fopen("patientdetails.dat", "w");
if (fp == NULL)
{
printf("Couldn't open file\n");
return -1;
}
else
{
printf("Do you want to enter record (y/n): ");
scanf("%c", &order);
while (order == 'y' || order == 'Y')
{
printf("Enter nic no.: ");
scanf("%s", nic);
printf("Enter name: ");
scanf("%s", name);
printf("Enter gender (m/f): ");
scanf(" %c", &gender);
printf("Enter tel no.: ");
scanf("%s", tel);
fprintf(fp, "%s %s %c %s", nic, name, gender, tel);
printf("Do you want to enter record (y/n): ");
scanf(" %c", &order);
}
fclose(fp);
}
return 0;
}
However, when I enter the info into the .dat file the gender shows up as a symbol instead and I just can't get around it.
.dat file image. Gender shows as symbol not m for male
edit:
This is my terminal input:
Do you want to enter record (y/n): y
Enter nic no.: 200007102766
Enter name: aruna
Enter gender (m/f): m
Enter tel no.: 0779426698
Do you want to enter record (y/n): n
The m for male, in the gender input shows up as a diamond with a question mark.
Hey, I wrote a c program to read my .dat file and it seems to work now. Thanks!