37 — Files: saving and reading again
Everything so far disappeared when the program ended. A file stays. Part 3 begins here.
From this guide on, the programs do not run on the site. There is nowhere inside a browser to keep a file, so a program that writes one fails on needmoreeasy.com. Try these on your own computer, after install.
Steps
Write it. Name the file and the text to put in it:
run it →write "today was a good day" to "diary.txt"diary.txtappears in the folder you ran the program from.Read it back into a name, then show that name:
run it →read "diary.txt" into memo show memoPut them together and it is one program:
run it →write "today was a good day" to "diary.txt" read "diary.txt" into memo show memoKorean is
"일기.txt" 파일에 "…"를 저장해and메모에 "일기.txt" 읽어서.This is the one place quotes appear. Sentence syntax has no quotes anywhere else. A file name is the exception because
diary.txtis not a word of your sentence — it is the name of something outside the program, and the quotes are what say "take this exactly as it stands".What somebody types can be saved too. Put the answer straight in:
run it →ask today What happened today? write today to "diary.txt" read "diary.txt" into memo show what was saved show memoRun it a second time and the first entry is overwritten. Adding to a file instead of replacing it is 44 — Log.
Try it yourself
Take the game from 23 — High score and write the best score to a file at the end. Then stop the program, start it again, and check the file is still there.
What you learned
write … to "name.txt"puts text in a file.read "name.txt" into …takes it back out.- File names are wrapped in quotes — the only place sentence syntax uses them.
- The file appears in the folder you ran the program from.
- Writing the same name again replaces what was there.
- A program that uses files does not run in a browser.