← Back to Lua Course | Chapter 8: File I/O | Lesson 3 of 7

Files पढ़ना

read method formats लेता है जैसे सब कुछ के लिए "a", एक line के लिए "l" और एक number के लिए "n"।

In this page:

  1. Files पढ़ना

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

lua
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
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि "l" newline रखेगा
  2. यह उम्मीद करना कि "a" अंत में nil return करेगा
  3. 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.