This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
isavegas's profile picture

Writing a variable to a file

Started by isavegas, 11 April 2013 - 08:15 AM
isavegas #1
Posted 11 April 2013 - 10:15 AM
I've tried everything I can, but it seems I can only write a hardcoded string to a file using fs.open().



t = fs.open("t","w")
t.write("Hello")
t.close
In this case, the string written to the file works perfectly fine.
The file it creates will simply say

Hello


h = Hello
t = fs.open("t2","w")
t.write(h)
t.close
However, when I try this, the file it creates is empty.

Isn't it supposed to write the value of the variable to the file? I've tried adding quotation marks around the variable, and it neatly writes the string into the file. It's as if it doesn't see variables, which makes saving flexible variables so they will withstand a reboot is extremely hard, if not impossible.
Also, I've found various bits of information on the forums that say that you should be able to use variables when using fs.write().
SadKingBilly #2
Posted 11 April 2013 - 10:33 AM
Well, in the code you provided, the problem is that h doesn't contain a string, since you forgot to put it in quotes. The line should be h = "Hello".
SuicidalSTDz #3
Posted 11 April 2013 - 11:00 AM
Being more technical:

If you just write Hello to a file, it will assume you are calling the variable Hello, which you obviously don't want to do. The reason the file is empty it because Hello is nil, so it wrote a nil value.
isavegas #4
Posted 11 April 2013 - 11:47 AM
Somehow, I've never had an issue with it before. O.o
Lol. Thanks for the help! I feel so stupid now. xD