/* * filechange.c */ #include #include int main(void) { FILE * fp; int count; float num; long position; if( (fp=fopen("numbers", "r+b"))==NULL ) // the + is essential { printf("filechange can't open file 'numbers' \n"); exit(1); } num=0.123456; printf("This program replaces the fifth number in the file\n"); printf("with the number 0.123456.\n"); if( fseek(fp, 4*(sizeof(float)), 0) !=0 ) { printf("fseek() failed/n"); exit(1); } position=ftell(fp); printf("The current position in the file is \n"); printf("%d bytes from the beginning.\n", position); if( fwrite(&num, sizeof(float), 1, fp) !=1 ) { printf("Write failed!\n"); exit(1); } if( fseek(fp, 0,0) != 0 ); { printf("fseek() failed"); exit(1); } num=0.0; count=0; printf("Number\tData\n"); while(~eof(fp)) { fread(&num, sizeof(float), 1, fp); if(!feof(fp)) { printf("%ld\t%.6f\n", count++, num); } } fclose(fp); return 0; }