Home » Archimedes archive » Acorn User » AU 1995-07.adf » !Regulars_Regulars » C/c/RandomB
C/c/RandomB
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
Tape/disk: | Home » Archimedes archive » Acorn User » AU 1995-07.adf » !Regulars_Regulars |
Filename: | C/c/RandomB |
Read OK: | ✔ |
File size: | 071D bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* C for Yourself - Acorn User July 1995 Demonstration of random access file handling, part II (using the 'update' mode) by Steve Mumford */ #include <stdio.h> void main(void) { FILE *pointer; char buffer[255]; int x; printf("Random Access file handling part II\n"); printf("-----------------------------------\n\n"); if ((pointer = fopen("<C$Dir>.datafile", "r+")) == 0) { printf("File could not be opened for update.\n"); return; } do { do { printf("Please enter a number from 1 to 5 to see a record:\n"); printf("Prefix the number with a minus sign to edit that record.\n"); printf("(Enter 0 to leave the program)\n"); scanf("%d",&x); while (getchar() != '\n'); /* This 'cleans' the input of scanf() */ if (x == 0) break; if (x < -5 || x > 5) { printf("Sorry - that number was out of range.\n"); } } while (x < -5 || x > 5); if (x > 0) { if (fseek(pointer, (x-1) * 16, SEEK_SET) != 0) { printf("fseek() failed to change position.\n"); return; } fgets(buffer, 17, pointer); puts(buffer); printf("\n"); } else if (x < 0) { x = -x; if (fseek(pointer, (x-1) * 16, SEEK_SET) != 0) { printf("fseek() failed to change position.\n"); return; } for (x = 0; x <= 15; x++) { buffer[x] = '\0'; } printf("Please enter string:\n"); gets(buffer); for (x = 0; x < 15; x++) { fputc(buffer[x], pointer); } fputc('\0', pointer); } } while (x != 0); if (fclose(pointer) != 0) { printf("File could not be closed after reading.\n"); } }
00000000 2f 2a 20 43 20 66 6f 72 20 59 6f 75 72 73 65 6c |/* C for Yoursel| 00000010 66 20 2d 20 41 63 6f 72 6e 20 55 73 65 72 20 4a |f - Acorn User J| 00000020 75 6c 79 20 31 39 39 35 0a 20 20 20 44 65 6d 6f |uly 1995. Demo| 00000030 6e 73 74 72 61 74 69 6f 6e 20 6f 66 20 72 61 6e |nstration of ran| 00000040 64 6f 6d 20 61 63 63 65 73 73 20 66 69 6c 65 20 |dom access file | 00000050 68 61 6e 64 6c 69 6e 67 2c 20 70 61 72 74 20 49 |handling, part I| 00000060 49 0a 20 20 20 28 75 73 69 6e 67 20 74 68 65 20 |I. (using the | 00000070 27 75 70 64 61 74 65 27 20 6d 6f 64 65 29 0a 20 |'update' mode). | 00000080 20 20 62 79 20 53 74 65 76 65 20 4d 75 6d 66 6f | by Steve Mumfo| 00000090 72 64 20 2a 2f 0a 0a 20 20 20 23 69 6e 63 6c 75 |rd */.. #inclu| 000000a0 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 0a 76 6f |de <stdio.h>..vo| 000000b0 69 64 0a 6d 61 69 6e 28 76 6f 69 64 29 0a 7b 0a |id.main(void).{.| 000000c0 20 20 20 46 49 4c 45 20 20 2a 70 6f 69 6e 74 65 | FILE *pointe| 000000d0 72 3b 0a 20 20 20 63 68 61 72 20 20 62 75 66 66 |r;. char buff| 000000e0 65 72 5b 32 35 35 5d 3b 0a 20 20 20 69 6e 74 20 |er[255];. int | 000000f0 20 20 78 3b 0a 0a 20 20 20 70 72 69 6e 74 66 28 | x;.. printf(| 00000100 22 52 61 6e 64 6f 6d 20 41 63 63 65 73 73 20 66 |"Random Access f| 00000110 69 6c 65 20 68 61 6e 64 6c 69 6e 67 20 70 61 72 |ile handling par| 00000120 74 20 49 49 5c 6e 22 29 3b 0a 20 20 20 70 72 69 |t II\n");. pri| 00000130 6e 74 66 28 22 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |ntf("-----------| 00000140 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d 2d |----------------| 00000150 2d 2d 2d 2d 2d 2d 2d 2d 5c 6e 5c 6e 22 29 3b 0a |--------\n\n");.| 00000160 0a 0a 20 20 20 69 66 20 28 28 70 6f 69 6e 74 65 |.. if ((pointe| 00000170 72 20 3d 20 66 6f 70 65 6e 28 22 3c 43 24 44 69 |r = fopen("<C$Di| 00000180 72 3e 2e 64 61 74 61 66 69 6c 65 22 2c 20 22 72 |r>.datafile", "r| 00000190 2b 22 29 29 20 3d 3d 20 30 29 20 7b 0a 20 20 20 |+")) == 0) {. | 000001a0 70 72 69 6e 74 66 28 22 46 69 6c 65 20 63 6f 75 |printf("File cou| 000001b0 6c 64 20 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 |ld not be opened| 000001c0 20 66 6f 72 20 75 70 64 61 74 65 2e 5c 6e 22 29 | for update.\n")| 000001d0 3b 0a 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 |;. return;. | 000001e0 7d 0a 0a 20 20 20 64 6f 20 7b 0a 20 20 20 20 20 |}.. do {. | 000001f0 20 64 6f 20 7b 0a 20 20 20 20 20 20 20 20 20 70 | do {. p| 00000200 72 69 6e 74 66 28 22 50 6c 65 61 73 65 20 65 6e |rintf("Please en| 00000210 74 65 72 20 61 20 6e 75 6d 62 65 72 20 66 72 6f |ter a number fro| 00000220 6d 20 31 20 74 6f 20 35 20 74 6f 20 73 65 65 20 |m 1 to 5 to see | 00000230 61 20 72 65 63 6f 72 64 3a 5c 6e 22 29 3b 0a 20 |a record:\n");. | 00000240 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 | printf("| 00000250 50 72 65 66 69 78 20 74 68 65 20 6e 75 6d 62 65 |Prefix the numbe| 00000260 72 20 77 69 74 68 20 61 20 6d 69 6e 75 73 20 73 |r with a minus s| 00000270 69 67 6e 20 74 6f 20 65 64 69 74 20 74 68 61 74 |ign to edit that| 00000280 20 72 65 63 6f 72 64 2e 5c 6e 22 29 3b 0a 20 20 | record.\n");. | 00000290 20 20 20 20 20 20 20 70 72 69 6e 74 66 28 22 28 | printf("(| 000002a0 45 6e 74 65 72 20 30 20 74 6f 20 6c 65 61 76 65 |Enter 0 to leave| 000002b0 20 74 68 65 20 70 72 6f 67 72 61 6d 29 5c 6e 22 | the program)\n"| 000002c0 29 3b 0a 20 20 20 20 20 20 20 20 20 73 63 61 6e |);. scan| 000002d0 66 28 22 25 64 22 2c 26 78 29 3b 0a 20 20 20 20 |f("%d",&x);. | 000002e0 20 20 20 20 20 77 68 69 6c 65 20 28 67 65 74 63 | while (getc| 000002f0 68 61 72 28 29 20 21 3d 20 27 5c 6e 27 29 3b 20 |har() != '\n'); | 00000300 2f 2a 20 54 68 69 73 20 27 63 6c 65 61 6e 73 27 |/* This 'cleans'| 00000310 20 74 68 65 20 69 6e 70 75 74 20 6f 66 20 73 63 | the input of sc| 00000320 61 6e 66 28 29 20 2a 2f 0a 20 20 20 20 20 20 20 |anf() */. | 00000330 20 20 69 66 20 28 78 20 3d 3d 20 30 29 20 62 72 | if (x == 0) br| 00000340 65 61 6b 3b 0a 20 20 20 20 20 20 20 20 20 69 66 |eak;. if| 00000350 20 28 78 20 3c 20 2d 35 20 7c 7c 20 78 20 3e 20 | (x < -5 || x > | 00000360 35 29 20 7b 0a 20 20 20 20 20 20 20 20 20 20 20 |5) {. | 00000370 20 70 72 69 6e 74 66 28 22 53 6f 72 72 79 20 2d | printf("Sorry -| 00000380 20 74 68 61 74 20 6e 75 6d 62 65 72 20 77 61 73 | that number was| 00000390 20 6f 75 74 20 6f 66 20 72 61 6e 67 65 2e 5c 6e | out of range.\n| 000003a0 22 29 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a 20 |");. }. | 000003b0 20 20 20 20 20 7d 20 77 68 69 6c 65 20 28 78 20 | } while (x | 000003c0 3c 20 2d 35 20 7c 7c 20 78 20 3e 20 35 29 3b 0a |< -5 || x > 5);.| 000003d0 0a 20 20 20 20 20 20 69 66 20 28 78 20 3e 20 30 |. if (x > 0| 000003e0 29 20 7b 0a 20 20 20 20 20 20 20 20 20 69 66 20 |) {. if | 000003f0 28 66 73 65 65 6b 28 70 6f 69 6e 74 65 72 2c 20 |(fseek(pointer, | 00000400 28 78 2d 31 29 20 2a 20 31 36 2c 20 53 45 45 4b |(x-1) * 16, SEEK| 00000410 5f 53 45 54 29 20 21 3d 20 30 29 20 7b 0a 20 20 |_SET) != 0) {. | 00000420 20 20 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 | printf| 00000430 28 22 66 73 65 65 6b 28 29 20 66 61 69 6c 65 64 |("fseek() failed| 00000440 20 74 6f 20 63 68 61 6e 67 65 20 70 6f 73 69 74 | to change posit| 00000450 69 6f 6e 2e 5c 6e 22 29 3b 0a 20 20 20 20 20 20 |ion.\n");. | 00000460 20 20 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 | return;. | 00000470 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 20 | }. | 00000480 20 20 66 67 65 74 73 28 62 75 66 66 65 72 2c 20 | fgets(buffer, | 00000490 31 37 2c 20 70 6f 69 6e 74 65 72 29 3b 0a 20 20 |17, pointer);. | 000004a0 20 20 20 20 20 20 20 70 75 74 73 28 62 75 66 66 | puts(buff| 000004b0 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 70 72 |er);. pr| 000004c0 69 6e 74 66 28 22 5c 6e 22 29 3b 0a 20 20 20 20 |intf("\n");. | 000004d0 20 20 7d 20 65 6c 73 65 20 69 66 20 28 78 20 3c | } else if (x <| 000004e0 20 30 29 20 7b 0a 20 20 20 20 20 20 20 20 20 78 | 0) {. x| 000004f0 20 3d 20 2d 78 3b 0a 20 20 20 20 20 20 20 20 20 | = -x;. | 00000500 69 66 20 28 66 73 65 65 6b 28 70 6f 69 6e 74 65 |if (fseek(pointe| 00000510 72 2c 20 28 78 2d 31 29 20 2a 20 31 36 2c 20 53 |r, (x-1) * 16, S| 00000520 45 45 4b 5f 53 45 54 29 20 21 3d 20 30 29 20 7b |EEK_SET) != 0) {| 00000530 0a 20 20 20 20 20 20 20 20 20 20 20 20 70 72 69 |. pri| 00000540 6e 74 66 28 22 66 73 65 65 6b 28 29 20 66 61 69 |ntf("fseek() fai| 00000550 6c 65 64 20 74 6f 20 63 68 61 6e 67 65 20 70 6f |led to change po| 00000560 73 69 74 69 6f 6e 2e 5c 6e 22 29 3b 0a 20 20 20 |sition.\n");. | 00000570 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e 3b | return;| 00000580 0a 20 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 |. }. | 00000590 20 20 20 20 20 66 6f 72 20 28 78 20 3d 20 30 3b | for (x = 0;| 000005a0 20 78 20 3c 3d 20 31 35 3b 20 78 2b 2b 29 20 7b | x <= 15; x++) {| 000005b0 0a 20 20 20 20 20 20 20 20 20 20 20 20 62 75 66 |. buf| 000005c0 66 65 72 5b 78 5d 20 3d 20 27 5c 30 27 3b 0a 20 |fer[x] = '\0';. | 000005d0 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20 20 | }. | 000005e0 0a 20 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 |. printf| 000005f0 28 22 50 6c 65 61 73 65 20 65 6e 74 65 72 20 73 |("Please enter s| 00000600 74 72 69 6e 67 3a 5c 6e 22 29 3b 0a 20 20 20 20 |tring:\n");. | 00000610 20 20 20 20 20 67 65 74 73 28 62 75 66 66 65 72 | gets(buffer| 00000620 29 3b 0a 20 20 20 20 20 20 0a 20 20 20 20 20 20 |);. . | 00000630 20 20 20 66 6f 72 20 28 78 20 3d 20 30 3b 20 78 | for (x = 0; x| 00000640 20 3c 20 31 35 3b 20 78 2b 2b 29 20 7b 0a 20 20 | < 15; x++) {. | 00000650 20 20 20 20 20 20 20 20 20 20 66 70 75 74 63 28 | fputc(| 00000660 62 75 66 66 65 72 5b 78 5d 2c 20 70 6f 69 6e 74 |buffer[x], point| 00000670 65 72 29 3b 0a 20 20 20 20 20 20 20 20 20 7d 0a |er);. }.| 00000680 20 20 20 20 20 20 20 20 20 66 70 75 74 63 28 27 | fputc('| 00000690 5c 30 27 2c 20 70 6f 69 6e 74 65 72 29 3b 0a 20 |\0', pointer);. | 000006a0 20 20 20 20 20 7d 0a 20 20 20 7d 20 77 68 69 6c | }. } whil| 000006b0 65 20 28 78 20 21 3d 20 30 29 3b 0a 0a 20 20 20 |e (x != 0);.. | 000006c0 69 66 20 28 66 63 6c 6f 73 65 28 70 6f 69 6e 74 |if (fclose(point| 000006d0 65 72 29 20 21 3d 20 30 29 20 7b 0a 20 20 20 20 |er) != 0) {. | 000006e0 20 20 70 72 69 6e 74 66 28 22 46 69 6c 65 20 63 | printf("File c| 000006f0 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c 6f 73 |ould not be clos| 00000700 65 64 20 61 66 74 65 72 20 72 65 61 64 69 6e 67 |ed after reading| 00000710 2e 5c 6e 22 29 3b 0a 20 20 20 7d 0a 7d |.\n");. }.}| 0000071d