13 — Files: save and read text
A value lives until the program ends. A file lives on after it. NME's bundled
file helper writes text to a file and reads it back with two easy lines.
Steps
Create
diary.nme:
run it →use file latest file_write("diary.txt", "Today is a good day") show file_read("diary.txt")use file latestloads the helper.file_writeputs the text into a new file, andfile_readreads it back.Run it:
nme run diaryThe console prints
Today is a good day. Look at the folder: adiary.txtfile now sits next to your program.Files are written in the folder you run
nmein, so keep the program and its files together.nme ris the shortcut fornme run:nme r diaryKorean loads the helper with
파일 사용 최신:
run it →파일 사용 최신 파일쓰기("일기.txt", "오늘은 좋은 날이에요") 말해 파일읽기("일기.txt")파일쓰기writes and파일읽기reads, exactly like the English names. Both vocabularies work in one file.
Try it yourself
Write your favorite food to food.txt, then read it back:
use file latest
file_write("food.txt", "Kimchi stew")
show file_read("food.txt")
run it →What you learned
use file latest/파일 사용 최신loads the bundled file helper.file_write(path, text)/파일쓰기(경로, 내용)saves text to a file.file_read(path)/파일읽기(경로)reads it back.- The file appears in the folder you run
nmein, next to the program.