Home » Archimedes archive » Acorn User » AU 1995-06.adf » !C_C » c/saveload
c/saveload
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/saveload |
Read OK: | ✔ |
File size: | 08B0 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* C for Yourself - Acorn User June 1995 Program to demonstrate writing different types of numbers to a file and reading them back in. By Steve Mumford */ #include <stdio.h> void main(void) { FILE *pointer; int var1, var2, var3, test1, test3, test4, n; float var4,test2; double var5, test5; if ((pointer = fopen("savefile", "w")) == 0) { printf("File could not be opened. (during save)\n"); return; } /* The above if statement checks whether the file was actually opened - if there had been a problem, we don't want to attempt to send data to a non-existent file. The pointer that fopen() returns is equal to zero if there's been a problem. */ var1 = 5; var2 = 47; var3 = 123; var4 = 3.456; var5 = 1.234567; n = fprintf(pointer,"%d %f %d\n",var1, var4, var2); if (n < 0) { printf("There was a problem with the save.\n"); } n = fprintf(pointer,"%d %lf\n", var3, var5); if (n < 0) { printf("There was a problem with the save.\n"); } /* The integer that printf() returns is negative if there's been a problem with the save operation. */ if (fclose(pointer) != 0) { printf("File could not be closed. (during save)\n"); } /* fclose() returns a value of zero if the file has been closed successfully. */ if ((pointer = fopen("savefile", "r")) == 0) { printf("File could not be opened. (during loading)\n"); return; } /* If the file couldn't be opened, it's dangerous to try to load anything from it, which is why the 'return' statement is included. */ n = fscanf(pointer,"%d %f %d",&test1, &test2, &test3); printf("Number of data items loaded for first fscanf() = : %d\n", n); /* fscanf() returns the number of data items it's successfully matched and stored - in the above case, this should be 3. */ n = fscanf(pointer,"%d %lf",&test4, &test5); printf("Number of data items loaded for second fscanf() = : %d\n", n); printf("Numbers loaded are: %d, %f and %d\n",test1,test2,test3); printf("And: %d & %lf", test4, test5); if (fclose(pointer) != 0) { printf("File could not be closed. (during loading)\n"); } return; }
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 50 72 6f 67 |une 1995. Prog| 00000030 72 61 6d 20 74 6f 20 64 65 6d 6f 6e 73 74 72 61 |ram to demonstra| 00000040 74 65 20 77 72 69 74 69 6e 67 20 64 69 66 66 65 |te writing diffe| 00000050 72 65 6e 74 20 74 79 70 65 73 20 6f 66 20 6e 75 |rent types of nu| 00000060 6d 62 65 72 73 0a 20 20 20 74 6f 20 61 20 66 69 |mbers. to a fi| 00000070 6c 65 20 61 6e 64 20 72 65 61 64 69 6e 67 20 74 |le and reading t| 00000080 68 65 6d 20 62 61 63 6b 20 69 6e 2e 0a 20 20 20 |hem back in.. | 00000090 42 79 20 53 74 65 76 65 20 4d 75 6d 66 6f 72 64 |By Steve Mumford| 000000a0 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65 20 3c 73 | */..#include <s| 000000b0 74 64 69 6f 2e 68 3e 0a 0a 76 6f 69 64 0a 6d 61 |tdio.h>..void.ma| 000000c0 69 6e 28 76 6f 69 64 29 0a 7b 0a 20 20 20 46 49 |in(void).{. FI| 000000d0 4c 45 20 20 20 2a 70 6f 69 6e 74 65 72 3b 0a 20 |LE *pointer;. | 000000e0 20 20 69 6e 74 20 20 20 20 76 61 72 31 2c 20 76 | int var1, v| 000000f0 61 72 32 2c 20 76 61 72 33 2c 20 74 65 73 74 31 |ar2, var3, test1| 00000100 2c 20 74 65 73 74 33 2c 20 74 65 73 74 34 2c 20 |, test3, test4, | 00000110 6e 3b 0a 20 20 20 66 6c 6f 61 74 20 20 76 61 72 |n;. float var| 00000120 34 2c 74 65 73 74 32 3b 0a 20 20 20 64 6f 75 62 |4,test2;. doub| 00000130 6c 65 20 76 61 72 35 2c 20 74 65 73 74 35 3b 0a |le var5, test5;.| 00000140 0a 20 20 20 69 66 20 28 28 70 6f 69 6e 74 65 72 |. if ((pointer| 00000150 20 3d 20 66 6f 70 65 6e 28 22 73 61 76 65 66 69 | = fopen("savefi| 00000160 6c 65 22 2c 20 22 77 22 29 29 20 3d 3d 20 30 29 |le", "w")) == 0)| 00000170 20 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 28 | {. printf(| 00000180 22 46 69 6c 65 20 63 6f 75 6c 64 20 6e 6f 74 20 |"File could not | 00000190 62 65 20 6f 70 65 6e 65 64 2e 20 28 64 75 72 69 |be opened. (duri| 000001a0 6e 67 20 73 61 76 65 29 5c 6e 22 29 3b 0a 20 20 |ng save)\n");. | 000001b0 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 20 7d | return;. }| 000001c0 0a 0a 20 20 20 2f 2a 20 54 68 65 20 61 62 6f 76 |.. /* The abov| 000001d0 65 20 69 66 20 73 74 61 74 65 6d 65 6e 74 20 63 |e if statement c| 000001e0 68 65 63 6b 73 20 77 68 65 74 68 65 72 20 74 68 |hecks whether th| 000001f0 65 20 66 69 6c 65 20 77 61 73 20 61 63 74 75 61 |e file was actua| 00000200 6c 6c 79 0a 20 20 20 6f 70 65 6e 65 64 20 2d 20 |lly. opened - | 00000210 69 66 20 74 68 65 72 65 20 68 61 64 20 62 65 65 |if there had bee| 00000220 6e 20 61 20 70 72 6f 62 6c 65 6d 2c 20 77 65 20 |n a problem, we | 00000230 64 6f 6e 27 74 20 77 61 6e 74 20 74 6f 20 61 74 |don't want to at| 00000240 74 65 6d 70 74 0a 20 20 20 74 6f 20 73 65 6e 64 |tempt. to send| 00000250 20 64 61 74 61 20 74 6f 20 61 20 6e 6f 6e 2d 65 | data to a non-e| 00000260 78 69 73 74 65 6e 74 20 66 69 6c 65 2e 20 54 68 |xistent file. Th| 00000270 65 20 70 6f 69 6e 74 65 72 20 74 68 61 74 20 66 |e pointer that f| 00000280 6f 70 65 6e 28 29 0a 20 20 20 72 65 74 75 72 6e |open(). return| 00000290 73 20 69 73 20 65 71 75 61 6c 20 74 6f 20 7a 65 |s is equal to ze| 000002a0 72 6f 20 69 66 20 74 68 65 72 65 27 73 20 62 65 |ro if there's be| 000002b0 65 6e 20 61 20 70 72 6f 62 6c 65 6d 2e 20 2a 2f |en a problem. */| 000002c0 0a 0a 20 20 20 76 61 72 31 20 3d 20 35 3b 0a 20 |.. var1 = 5;. | 000002d0 20 20 76 61 72 32 20 3d 20 34 37 3b 0a 20 20 20 | var2 = 47;. | 000002e0 76 61 72 33 20 3d 20 31 32 33 3b 0a 20 20 20 76 |var3 = 123;. v| 000002f0 61 72 34 20 3d 20 33 2e 34 35 36 3b 0a 20 20 20 |ar4 = 3.456;. | 00000300 76 61 72 35 20 3d 20 31 2e 32 33 34 35 36 37 3b |var5 = 1.234567;| 00000310 0a 0a 20 20 20 6e 20 3d 20 66 70 72 69 6e 74 66 |.. n = fprintf| 00000320 28 70 6f 69 6e 74 65 72 2c 22 25 64 20 25 66 20 |(pointer,"%d %f | 00000330 25 64 5c 6e 22 2c 76 61 72 31 2c 20 76 61 72 34 |%d\n",var1, var4| 00000340 2c 20 76 61 72 32 29 3b 0a 20 20 20 69 66 20 28 |, var2);. if (| 00000350 6e 20 3c 20 30 29 20 7b 0a 20 20 20 20 20 20 70 |n < 0) {. p| 00000360 72 69 6e 74 66 28 22 54 68 65 72 65 20 77 61 73 |rintf("There was| 00000370 20 61 20 70 72 6f 62 6c 65 6d 20 77 69 74 68 20 | a problem with | 00000380 74 68 65 20 73 61 76 65 2e 5c 6e 22 29 3b 0a 20 |the save.\n");. | 00000390 20 20 7d 0a 20 20 20 6e 20 3d 20 66 70 72 69 6e | }. n = fprin| 000003a0 74 66 28 70 6f 69 6e 74 65 72 2c 22 25 64 20 25 |tf(pointer,"%d %| 000003b0 6c 66 5c 6e 22 2c 20 76 61 72 33 2c 20 76 61 72 |lf\n", var3, var| 000003c0 35 29 3b 0a 20 20 20 69 66 20 28 6e 20 3c 20 30 |5);. if (n < 0| 000003d0 29 20 7b 0a 20 20 20 20 20 20 70 72 69 6e 74 66 |) {. printf| 000003e0 28 22 54 68 65 72 65 20 77 61 73 20 61 20 70 72 |("There was a pr| 000003f0 6f 62 6c 65 6d 20 77 69 74 68 20 74 68 65 20 73 |oblem with the s| 00000400 61 76 65 2e 5c 6e 22 29 3b 0a 20 20 20 7d 0a 0a |ave.\n");. }..| 00000410 20 20 20 2f 2a 20 54 68 65 20 69 6e 74 65 67 65 | /* The intege| 00000420 72 20 74 68 61 74 20 70 72 69 6e 74 66 28 29 20 |r that printf() | 00000430 72 65 74 75 72 6e 73 20 69 73 20 6e 65 67 61 74 |returns is negat| 00000440 69 76 65 20 69 66 20 74 68 65 72 65 27 73 0a 20 |ive if there's. | 00000450 20 20 62 65 65 6e 20 61 20 70 72 6f 62 6c 65 6d | been a problem| 00000460 20 77 69 74 68 20 74 68 65 20 73 61 76 65 20 6f | with the save o| 00000470 70 65 72 61 74 69 6f 6e 2e 20 2a 2f 0a 0a 20 20 |peration. */.. | 00000480 20 69 66 20 28 66 63 6c 6f 73 65 28 70 6f 69 6e | if (fclose(poin| 00000490 74 65 72 29 20 21 3d 20 30 29 20 7b 0a 20 20 20 |ter) != 0) {. | 000004a0 20 20 20 70 72 69 6e 74 66 28 22 46 69 6c 65 20 | printf("File | 000004b0 63 6f 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c 6f |could not be clo| 000004c0 73 65 64 2e 20 28 64 75 72 69 6e 67 20 73 61 76 |sed. (during sav| 000004d0 65 29 5c 6e 22 29 3b 0a 20 20 20 7d 0a 0a 20 20 |e)\n");. }.. | 000004e0 20 2f 2a 20 66 63 6c 6f 73 65 28 29 20 72 65 74 | /* fclose() ret| 000004f0 75 72 6e 73 20 61 20 76 61 6c 75 65 20 6f 66 20 |urns a value of | 00000500 7a 65 72 6f 20 69 66 20 74 68 65 20 66 69 6c 65 |zero if the file| 00000510 20 68 61 73 20 62 65 65 6e 20 63 6c 6f 73 65 64 | has been closed| 00000520 0a 20 20 20 73 75 63 63 65 73 73 66 75 6c 6c 79 |. successfully| 00000530 2e 20 2a 2f 0a 0a 20 20 20 69 66 20 28 28 70 6f |. */.. if ((po| 00000540 69 6e 74 65 72 20 3d 20 66 6f 70 65 6e 28 22 73 |inter = fopen("s| 00000550 61 76 65 66 69 6c 65 22 2c 20 22 72 22 29 29 20 |avefile", "r")) | 00000560 3d 3d 20 30 29 20 7b 0a 20 20 20 20 20 20 70 72 |== 0) {. pr| 00000570 69 6e 74 66 28 22 46 69 6c 65 20 63 6f 75 6c 64 |intf("File could| 00000580 20 6e 6f 74 20 62 65 20 6f 70 65 6e 65 64 2e 20 | not be opened. | 00000590 28 64 75 72 69 6e 67 20 6c 6f 61 64 69 6e 67 29 |(during loading)| 000005a0 5c 6e 22 29 3b 0a 20 20 20 20 20 20 72 65 74 75 |\n");. retu| 000005b0 72 6e 3b 0a 20 20 20 7d 0a 0a 20 20 20 2f 2a 20 |rn;. }.. /* | 000005c0 49 66 20 74 68 65 20 66 69 6c 65 20 63 6f 75 6c |If the file coul| 000005d0 64 6e 27 74 20 62 65 20 6f 70 65 6e 65 64 2c 20 |dn't be opened, | 000005e0 69 74 27 73 20 64 61 6e 67 65 72 6f 75 73 20 74 |it's dangerous t| 000005f0 6f 20 74 72 79 20 74 6f 20 6c 6f 61 64 0a 20 20 |o try to load. | 00000600 20 61 6e 79 74 68 69 6e 67 20 66 72 6f 6d 20 69 | anything from i| 00000610 74 2c 20 77 68 69 63 68 20 69 73 20 77 68 79 20 |t, which is why | 00000620 74 68 65 20 27 72 65 74 75 72 6e 27 20 73 74 61 |the 'return' sta| 00000630 74 65 6d 65 6e 74 20 69 73 0a 20 20 20 69 6e 63 |tement is. inc| 00000640 6c 75 64 65 64 2e 20 2a 2f 20 0a 0a 20 20 20 6e |luded. */ .. n| 00000650 20 3d 20 66 73 63 61 6e 66 28 70 6f 69 6e 74 65 | = fscanf(pointe| 00000660 72 2c 22 25 64 20 25 66 20 25 64 22 2c 26 74 65 |r,"%d %f %d",&te| 00000670 73 74 31 2c 20 26 74 65 73 74 32 2c 20 26 74 65 |st1, &test2, &te| 00000680 73 74 33 29 3b 0a 20 20 20 70 72 69 6e 74 66 28 |st3);. printf(| 00000690 22 4e 75 6d 62 65 72 20 6f 66 20 64 61 74 61 20 |"Number of data | 000006a0 69 74 65 6d 73 20 6c 6f 61 64 65 64 20 66 6f 72 |items loaded for| 000006b0 20 66 69 72 73 74 20 66 73 63 61 6e 66 28 29 20 | first fscanf() | 000006c0 3d 20 3a 20 25 64 5c 6e 22 2c 20 6e 29 3b 0a 0a |= : %d\n", n);..| 000006d0 20 20 20 2f 2a 20 66 73 63 61 6e 66 28 29 20 72 | /* fscanf() r| 000006e0 65 74 75 72 6e 73 20 74 68 65 20 6e 75 6d 62 65 |eturns the numbe| 000006f0 72 20 6f 66 20 64 61 74 61 20 69 74 65 6d 73 20 |r of data items | 00000700 69 74 27 73 20 73 75 63 63 65 73 73 66 75 6c 6c |it's successfull| 00000710 79 0a 20 20 20 6d 61 74 63 68 65 64 20 61 6e 64 |y. matched and| 00000720 20 73 74 6f 72 65 64 20 2d 20 69 6e 20 74 68 65 | stored - in the| 00000730 20 61 62 6f 76 65 20 63 61 73 65 2c 20 74 68 69 | above case, thi| 00000740 73 20 73 68 6f 75 6c 64 20 62 65 20 33 2e 20 2a |s should be 3. *| 00000750 2f 0a 0a 20 20 20 6e 20 3d 20 66 73 63 61 6e 66 |/.. n = fscanf| 00000760 28 70 6f 69 6e 74 65 72 2c 22 25 64 20 25 6c 66 |(pointer,"%d %lf| 00000770 22 2c 26 74 65 73 74 34 2c 20 26 74 65 73 74 35 |",&test4, &test5| 00000780 29 3b 0a 20 20 20 70 72 69 6e 74 66 28 22 4e 75 |);. printf("Nu| 00000790 6d 62 65 72 20 6f 66 20 64 61 74 61 20 69 74 65 |mber of data ite| 000007a0 6d 73 20 6c 6f 61 64 65 64 20 66 6f 72 20 73 65 |ms loaded for se| 000007b0 63 6f 6e 64 20 66 73 63 61 6e 66 28 29 20 3d 20 |cond fscanf() = | 000007c0 3a 20 25 64 5c 6e 22 2c 20 6e 29 3b 0a 20 20 20 |: %d\n", n);. | 000007d0 70 72 69 6e 74 66 28 22 4e 75 6d 62 65 72 73 20 |printf("Numbers | 000007e0 6c 6f 61 64 65 64 20 61 72 65 3a 20 25 64 2c 20 |loaded are: %d, | 000007f0 25 66 20 61 6e 64 20 25 64 5c 6e 22 2c 74 65 73 |%f and %d\n",tes| 00000800 74 31 2c 74 65 73 74 32 2c 74 65 73 74 33 29 3b |t1,test2,test3);| 00000810 0a 20 20 20 70 72 69 6e 74 66 28 22 41 6e 64 3a |. printf("And:| 00000820 20 25 64 20 26 20 25 6c 66 22 2c 20 74 65 73 74 | %d & %lf", test| 00000830 34 2c 20 74 65 73 74 35 29 3b 0a 0a 20 20 20 69 |4, test5);.. i| 00000840 66 20 28 66 63 6c 6f 73 65 28 70 6f 69 6e 74 65 |f (fclose(pointe| 00000850 72 29 20 21 3d 20 30 29 20 7b 0a 20 20 20 20 20 |r) != 0) {. | 00000860 20 70 72 69 6e 74 66 28 22 46 69 6c 65 20 63 6f | printf("File co| 00000870 75 6c 64 20 6e 6f 74 20 62 65 20 63 6c 6f 73 65 |uld not be close| 00000880 64 2e 20 28 64 75 72 69 6e 67 20 6c 6f 61 64 69 |d. (during loadi| 00000890 6e 67 29 5c 6e 22 29 3b 0a 20 20 20 7d 0a 20 20 |ng)\n");. }. | 000008a0 20 72 65 74 75 72 6e 3b 20 20 20 20 20 20 0a 7d | return; .}| 000008b0