Files में लिखना
write method strings और numbers को बिना newlines add किए एक file में save करता है।
In this page:
Files में लिखना
file:write किसी भी संख्या में strings और numbers accept करता है और इन्हें exactly दिए गए रूप में लिखता है, इसलिए \n खुद include करें। यह file handle return करता है ताकि calls chain हो सकें। Numbers usual format से convert होती हैं, और दूसरे types को पहले tostring से convert करना ज़रूरी है।
Note:
tidy, structured lines के लिए write के साथ string.format इस्तेमाल करें।
उदाहरण: Writing to files
local path = "/tmp/lua_demo_write.txt"
local f = assert(io.open(path, "w"))
f:write("line one\n")
f:write("line ", 2, "\n"):write(string.format("pi is %.2f\n", math.pi))
f:close()
local r = assert(io.open(path, "r"))
print(r:read("a"))
r:close()
-- Output:
-- line one
-- line 2
-- pi is 3.14
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- यह उम्मीद करना कि write एक newline add करेगा
- write को एक table या nil pass करना
- f.write("x") में colon की बजाय dot इस्तेमाल करना
चैप्टर सारांश
- write newlines add नहीं करता
- यह strings और numbers accept करता है
- यह chaining के लिए file return करता है
- colon syntax f:write इस्तेमाल करें
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: