Solution for Exercise 22-7
ex2207
#include <stdio.h> #include <stdlib.h> int main() { FILE *handle; int highscore; handle = fopen("scores.dat","w"); if(!handle) { puts("File error!"); exit(1); } printf("What is your high score? "); scanf("%d",&highscore); fwrite(&highscore,sizeof(int),1,handle); fclose(handle); puts("Score saved"); return(0); }
Output
What is your high score? 10000
Score saved
Notes
* The fwrite() function saves the int value in variable highscore
to a file. The value is saved as an int — a four byte binary value. (Well, four bytes on most computers.)
Copyright © 1997-2024 by QPBC.
All rights reserved