Jump to content




fs.open a variable file?

lua help

  • You cannot reply to this topic
6 replies to this topic

#1 andrewverry49000

  • New Members
  • 3 posts

Posted 04 October 2012 - 12:42 PM

I am trying to make a file open and write using a path variable in a string.


so, I have

filename = nil
write"filename: "
input = read()
filename = input
h = fs.open("Mydir/filename")

filename being the variable, i have been at this for 3 hours now, and cannot figure it out.
Any help would be appreciated

Thanks,
Andrew

#2 Fatal_Exception

  • New Members
  • 105 posts

Posted 04 October 2012 - 01:08 PM

One way is to simply concatenate the strings:

local path = "/whatever/your/path/is/"
local filename = "myfile.txt"

file = fs.open(path .. filename, mode) -- where mode is "r", "w" etc.


#3 Mtdj2

  • Members
  • 66 posts
  • LocationBehind you

Posted 04 October 2012 - 01:14 PM

Try looking at this post. (It is not mine.)
Click me!

#4 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 04 October 2012 - 01:40 PM

Well to shorten up all of your extra variables, you could simply do
write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename) -- dont have quotes around your variable


#5 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 25 October 2012 - 11:29 PM

View Postjag_e_nummer_ett, on 04 October 2012 - 01:40 PM, said:

Well to shorten up all of your extra variables, you could simply do
write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename) -- dont have quotes around your variable
How do you do the , "w" thing to write to the file?

#6 jag

  • Members
  • 533 posts
  • LocationStockholm, Sweden

Posted 25 October 2012 - 11:35 PM

View PostHithere__, on 25 October 2012 - 11:29 PM, said:

View Postjag_e_nummer_ett, on 04 October 2012 - 01:40 PM, said:

Well to shorten up all of your extra variables, you could simply do
write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename) -- dont have quotes around your variable
How do you do the , "w" thing to write to the file?

fs.open(path, mode)
Wiki: http://computercraft.info/wiki/index.php?title=Fs.open

#7 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 25 October 2012 - 11:44 PM

write"Filename: "
filename = read()
h = fs.open("Mydir/"..filename, "w")
h.write("rofl")
h.close()






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users