Files पढ़ना
read method formats लेता है जैसे सब कुछ के लिए "a", एक line के लिए "l" और एक number के लिए "n"।
In this page:
Files पढ़ना
file:read "a" जैसे formats accept करता है पूरी बाकी file के लिए, "l" अगली line के लिए इसकी newline के बिना, "L" newline रखने के लिए, "n" एक number के लिए, और एक plain integer उतने bytes के लिए। file के अंत पर read nil return करता है, सिवाय इसके कि "a" एक empty string return करता है। आप कई formats pass कर सकते हैं और कई results पा सकते हैं।
Note:
Lua 5.3 में formats बिना star के लिखे जाते हैं, हालाँकि older "*a" style अभी भी accepted है।
उदाहरण: Reading files
local path = "/tmp/lua_demo_read.txt"
local w = assert(io.open(path, "w"))
w:write("first line\n42 3.5\nlast")
w:close()
local f = assert(io.open(path, "r"))
print(f:read("l"))
print(f:read("n", "n"))
print(f:read(1) == "\n")
print(f:read("a"))
print(f:read("a") == "", f:read("l"))
f:close()
-- Output:
-- first line
-- 42 3.5
-- true
-- last
-- true nil
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- यह उम्मीद करना कि "l" newline रखेगा
- यह उम्मीद करना कि "a" अंत में nil return करेगा
- non-numeric text से "n" के साथ एक number पढ़ना
चैप्टर सारांश
- "a" सब कुछ पढ़ता है
- "l" बिना newline एक line पढ़ता है
- "n" एक number पढ़ता है
- एक count उतने bytes पढ़ता है
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: