Home » Archimedes archive » Acorn User » AU 1995-06.adf » !C_C » c/readarray
c/readarray
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-06.adf » !C_C |
Filename: | c/readarray |
Read OK: | ✔ |
File size: | 0468 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* C for Yourself - Acorn User June 1995 This program reads in a list of numbers from a handwritten text file and prints them on screen, using feof() to determine the end of the file. By Steve Mumford */ #include <stdio.h> void main(void) { int array[50]; char *filename = "<C$Dir>.testfile"; FILE *handle; int x, loop = 0; printf("This program reads numbers from a file and displays them on screen.\n"); printf("Although the program doesn't know how many numbers there are in the file,\n"); printf("the use of the feof() command allows it to keep loading until it reaches\n"); printf("the end of the list.\n\n"); if ((handle = fopen(filename, "r")) == 0) { printf("Sorry - the file %s could not be opened.\n", filename); return; } while (feof(handle) == 0) { x = fscanf(handle, "%d", &(array[loop])); if (x > 0) { loop++; } } for(x = 0; x < loop; x++) { printf("Number %d is: %d\n", x+1, array[x]); } if (fclose(handle) != 0) { printf("File couldn't be closed.\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 6e 65 20 31 39 39 35 0a 20 20 20 54 68 69 73 |une 1995. This| 00000030 20 70 72 6f 67 72 61 6d 20 72 65 61 64 73 20 69 | program reads i| 00000040 6e 20 61 20 6c 69 73 74 20 6f 66 20 6e 75 6d 62 |n a list of numb| 00000050 65 72 73 20 66 72 6f 6d 20 61 20 68 61 6e 64 77 |ers from a handw| 00000060 72 69 74 74 65 6e 20 74 65 78 74 0a 20 20 20 66 |ritten text. f| 00000070 69 6c 65 20 61 6e 64 20 70 72 69 6e 74 73 20 74 |ile and prints t| 00000080 68 65 6d 20 6f 6e 20 73 63 72 65 65 6e 2c 20 75 |hem on screen, u| 00000090 73 69 6e 67 20 66 65 6f 66 28 29 20 74 6f 20 64 |sing feof() to d| 000000a0 65 74 65 72 6d 69 6e 65 20 74 68 65 0a 20 20 20 |etermine the. | 000000b0 65 6e 64 20 6f 66 20 74 68 65 20 66 69 6c 65 2e |end of the file.| 000000c0 0a 20 20 20 42 79 20 53 74 65 76 65 20 4d 75 6d |. By Steve Mum| 000000d0 66 6f 72 64 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 |ford */..#includ| 000000e0 65 20 3c 73 74 64 69 6f 2e 68 3e 0a 0a 76 6f 69 |e <stdio.h>..voi| 000000f0 64 0a 6d 61 69 6e 28 76 6f 69 64 29 0a 7b 0a 20 |d.main(void).{. | 00000100 20 20 20 69 6e 74 20 61 72 72 61 79 5b 35 30 5d | int array[50]| 00000110 3b 0a 20 20 20 20 63 68 61 72 20 20 20 20 2a 66 |;. char *f| 00000120 69 6c 65 6e 61 6d 65 20 3d 20 22 3c 43 24 44 69 |ilename = "<C$Di| 00000130 72 3e 2e 74 65 73 74 66 69 6c 65 22 3b 0a 20 20 |r>.testfile";. | 00000140 20 20 46 49 4c 45 20 20 20 20 2a 68 61 6e 64 6c | FILE *handl| 00000150 65 3b 0a 20 20 20 20 69 6e 74 20 78 2c 20 6c 6f |e;. int x, lo| 00000160 6f 70 20 3d 20 30 3b 0a 0a 20 20 20 20 70 72 69 |op = 0;.. pri| 00000170 6e 74 66 28 22 54 68 69 73 20 70 72 6f 67 72 61 |ntf("This progra| 00000180 6d 20 72 65 61 64 73 20 6e 75 6d 62 65 72 73 20 |m reads numbers | 00000190 66 72 6f 6d 20 61 20 66 69 6c 65 20 61 6e 64 20 |from a file and | 000001a0 64 69 73 70 6c 61 79 73 20 74 68 65 6d 20 6f 6e |displays them on| 000001b0 20 73 63 72 65 65 6e 2e 5c 6e 22 29 3b 0a 20 20 | screen.\n");. | 000001c0 20 20 70 72 69 6e 74 66 28 22 41 6c 74 68 6f 75 | printf("Althou| 000001d0 67 68 20 74 68 65 20 70 72 6f 67 72 61 6d 20 64 |gh the program d| 000001e0 6f 65 73 6e 27 74 20 6b 6e 6f 77 20 68 6f 77 20 |oesn't know how | 000001f0 6d 61 6e 79 20 6e 75 6d 62 65 72 73 20 74 68 65 |many numbers the| 00000200 72 65 20 61 72 65 20 69 6e 20 74 68 65 20 66 69 |re are in the fi| 00000210 6c 65 2c 5c 6e 22 29 3b 0a 20 20 20 20 70 72 69 |le,\n");. pri| 00000220 6e 74 66 28 22 74 68 65 20 75 73 65 20 6f 66 20 |ntf("the use of | 00000230 74 68 65 20 66 65 6f 66 28 29 20 63 6f 6d 6d 61 |the feof() comma| 00000240 6e 64 20 61 6c 6c 6f 77 73 20 69 74 20 74 6f 20 |nd allows it to | 00000250 6b 65 65 70 20 6c 6f 61 64 69 6e 67 20 75 6e 74 |keep loading unt| 00000260 69 6c 20 69 74 20 72 65 61 63 68 65 73 5c 6e 22 |il it reaches\n"| 00000270 29 3b 0a 20 20 20 20 70 72 69 6e 74 66 28 22 74 |);. printf("t| 00000280 68 65 20 65 6e 64 20 6f 66 20 74 68 65 20 6c 69 |he end of the li| 00000290 73 74 2e 5c 6e 5c 6e 22 29 3b 0a 0a 20 20 20 20 |st.\n\n");.. | 000002a0 69 66 20 28 28 68 61 6e 64 6c 65 20 3d 20 66 6f |if ((handle = fo| 000002b0 70 65 6e 28 66 69 6c 65 6e 61 6d 65 2c 20 22 72 |pen(filename, "r| 000002c0 22 29 29 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 |")) == 0) {. | 000002d0 20 20 20 20 70 72 69 6e 74 66 28 22 53 6f 72 72 | printf("Sorr| 000002e0 79 20 2d 20 74 68 65 20 66 69 6c 65 20 25 73 20 |y - the file %s | 000002f0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 6f 70 65 |could not be ope| 00000300 6e 65 64 2e 5c 6e 22 2c 20 66 69 6c 65 6e 61 6d |ned.\n", filenam| 00000310 65 29 3b 0a 20 20 20 20 20 20 20 20 72 65 74 75 |e);. retu| 00000320 72 6e 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 77 |rn;. }.. w| 00000330 68 69 6c 65 20 28 66 65 6f 66 28 68 61 6e 64 6c |hile (feof(handl| 00000340 65 29 20 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 |e) == 0) {. | 00000350 20 20 20 78 20 3d 20 66 73 63 61 6e 66 28 68 61 | x = fscanf(ha| 00000360 6e 64 6c 65 2c 20 22 25 64 22 2c 20 26 28 61 72 |ndle, "%d", &(ar| 00000370 72 61 79 5b 6c 6f 6f 70 5d 29 29 3b 0a 20 20 20 |ray[loop]));. | 00000380 20 20 20 20 20 69 66 20 28 78 20 3e 20 30 29 20 | if (x > 0) | 00000390 7b 0a 20 20 20 20 20 20 20 20 20 20 20 20 6c 6f |{. lo| 000003a0 6f 70 2b 2b 3b 0a 20 20 20 20 20 20 20 20 7d 0a |op++;. }.| 000003b0 20 20 20 20 7d 0a 0a 20 20 20 20 66 6f 72 28 78 | }.. for(x| 000003c0 20 3d 20 30 3b 20 78 20 3c 20 6c 6f 6f 70 3b 20 | = 0; x < loop; | 000003d0 78 2b 2b 29 20 7b 0a 20 20 20 20 20 20 20 20 70 |x++) {. p| 000003e0 72 69 6e 74 66 28 22 4e 75 6d 62 65 72 20 25 64 |rintf("Number %d| 000003f0 20 69 73 3a 20 25 64 5c 6e 22 2c 20 78 2b 31 2c | is: %d\n", x+1,| 00000400 20 61 72 72 61 79 5b 78 5d 29 3b 0a 20 20 20 20 | array[x]);. | 00000410 7d 0a 0a 20 20 20 20 69 66 20 28 66 63 6c 6f 73 |}.. if (fclos| 00000420 65 28 68 61 6e 64 6c 65 29 20 21 3d 20 30 29 20 |e(handle) != 0) | 00000430 7b 0a 20 20 20 20 20 20 20 20 70 72 69 6e 74 66 |{. printf| 00000440 28 22 46 69 6c 65 20 63 6f 75 6c 64 6e 27 74 20 |("File couldn't | 00000450 62 65 20 63 6c 6f 73 65 64 2e 5c 6e 22 29 3b 0a |be closed.\n");.| 00000460 20 20 20 20 7d 0a 0a 7d | }..}| 00000468