just trying to run and understand this example from the book. the code compiles and runs correctly but i get weird results in the file. the account number i enter appears as a symbol in file, and the double value i enter appears something like this in the file - ÌÌÌ£#¹ü‡t @. and records are placed randomly (i guess that's the result of random access file?). why is all of these?
Code:
struct clientData { int account;
char firstName[10];
char lastName[15];
double balance;
};
int main() {
struct clientData client = {0, "", "", 0.0};
FILE *ptF;
if ((ptF = fopen("credit.txt", "rb+"))==NULL) {
printf("File could not be opened.\n");
} else {
printf("Enter account 1 to 100, 0 to end:\n? ");
scanf("%d", &client.account);
while (client.account != 0) {
printf("Enter first, last name, balance: ");
fscanf(stdin, "%s%s%lf", client.firstName,
client.lastName, &client.balance);
fseek(ptF, (client.account - 1)*sizeof(struct clientData), SEEK_SET);
fwrite(&client, sizeof(struct clientData), 1, ptF);
printf( "Enter account number\n? " );
scanf("%d", &client.account);
}
fclose(ptF);
}
getch();
return 0;
}