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

Default Files, os.remove और os.rename

io.write, io.read, और इनके जैसे default files इस्तेमाल करते हैं, और os delete और rename handle करता है।

Default Files, os.remove, और os.rename

io.output(path) io.write को एक file की ओर redirect करता है और io.input(path) io.read को redirect करता है, जबकि io.stdout और io.stderr हमेशा available हैं। os.rename(old, new) और os.remove(path) files manage करते हैं और success पर true, या failure पर nil plus एक message return करते हैं। io.output को कोई argument न देना current default output file return करता है।

Note: फिर से screen पर print करने से पहले io.output(io.stdout) से default output restore करें।

उदाहरण: Default files, os.remove and os.rename

lua
local a, b = "/tmp/lua_demo_a.txt", "/tmp/lua_demo_b.txt"
io.output(a)
io.write("written through default output\n")
io.close()
io.output(io.stdout)

print(os.rename(a, b))
io.input(b)
print(io.read("l"))
io.close(io.input())
io.input(io.stdin)

print(os.remove(b))
print(select("#", os.remove(b)))

-- Output:
-- true
-- written through default output
-- true
-- 3
{# 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. redirect करने के बाद io.output restore करना भूल जाना
  2. एक exist न करने वाली file पर os.remove इस्तेमाल करना और failure ignore करना
  3. io.write को file:write से confuse करना
चैप्टर सारांश
  • io.output io.write redirect करता है
  • io.input io.read redirect करता है
  • os.rename और os.remove true या nil plus एक message return करते हैं
  • io.stdout और io.stderr predefined हैं
🔒

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.