// Test several of the file functions. string tmp = "fileTest.tmp"; string tmp2 = "fileTest.tmp2"; string numbers = "0123456789"; string letters = "abcdefghijklmnopqrstuvwxyz"; string punctuation = "~!@#$$%^&*()_+=-}{,./?.,;:'\"\\|`"; /* Create file with numbers followed by letters */ file f = fileOpen(tmp, "w"); f.write(numbers); int numCount = f.tell(); f.write(letters); int letCount = f.tell() - numCount; f.close(); print("There are $numCount numbers and $letCount letters."); /* Reopen file to read, and seek to get letters and numbers. */ f = fileOpen(tmp, "r"); f.seek(numCount); string let = f.read(letCount); print("The letters are $let"); f.seek(0); string num = f.read(numCount); print("The numbers are $num"); /* Rewind file, and then do a print/skip loop. */ f.seek(0); prin("Every other number and letter: "); for (i in 0 til (numbers.size+letters.size)/2) { f.skip(1); string c = f.read(1); prin(c); } print(""); /* Seek from end too. */ f.seek(-5, 1); string last5 = f.read(5); print("The last five letters are $last5"); /* Now we implicitly close the file, just by reusing the f * var in a new file-open call, this time to append. * We add the punctuation marks. */ f = fileOpen(tmp, "a"); print("I've got " + punctuation.size + " punctuation marks: $punctuation"); f.write(punctuation); /* f.write(letters); f.close(); */ f = fileOpen(tmp, "r+"); print("I like upper case better too."); f.seek(10); string LETTERS = f.read(letters.size); LETTERS = LETTERS.upper(); f.skip(-letters.size); f.write(LETTERS); f.flush(); f.close(); string perlish = fileReadAll(tmp); print(perlish); print("That's better, it's more like Perl now."); // print("Are these lower case: $letters?"); print("Let's start over."); f = fileOpen(tmp, "w+"); f.write(letters+LETTERS); f.skip(-LETTERS.size); f.writeNow(numbers); print("How does this look?"); f.close(); print(fileReadAll(tmp)); // Do some binary operations. f = fileOpen(tmp, "w"); for (byte i=5; i<10; ++i) f.writeByte(i); f.close(); f = fileOpen(tmp); for (byte i=0; i<5; ++i) print(f.readByte()); f.close(); f = fileOpen(tmp, "w"); for (byte i=15; i<20; ++i) f.writeShort(i); f.close(); f = fileOpen(tmp); for (short i=0; i<5; ++i) print(f.readShort()); f.close(); f = fileOpen(tmp, "w"); for (int i=25; i<30; ++i) f.writeInt(i); f.close(); f = fileOpen(tmp); for (int i=0; i<5; ++i) print(f.readInt()); f.close(); f = fileOpen(tmp, "w"); for (long i=35; i<40; ++i) f.writeLong(i); f.close(); f = fileOpen(tmp); for (long i=0; i<5; ++i) print(f.readLong()); f.close(); f = fileOpen(tmp, "w"); for (float i=45; i<50; ++i) f.writeFloat(i); f.close(); f = fileOpen(tmp); for (float i=0; i<5; ++i) print(f.readFloat()); f.close(); f = fileOpen(tmp, "w"); for (double i=55; i<60; ++i) f.writeDouble(i); f.close(); f = fileOpen(tmp); for (double i=0; i<5; ++i) print(f.readDouble()); f.close(); // DO file moves and renames. bit t1Exists = fileExists(tmp), t2Exists = fileExists(tmp2); print("exists: $tmp $t1Exists, $tmp2 $t2Exists"); fileRename(tmp, tmp2); t1Exists = fileExists(tmp), t2Exists = fileExists(tmp2); print("After renaming $tmp to $tmp2"); print("exists: $tmp $t1Exists, $tmp2 $t2Exists"); fileRemove(tmp2); t1Exists = fileExists(tmp), t2Exists = fileExists(tmp2); print("After removing $tmp2"); print("exists: $tmp $t1Exists, $tmp2 $t2Exists"); print("Ok, I'm done now.");