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

Appending और Seeking

Append mode अंत में add करता है, और seek read या write position move करता है।

Append और Seek करना

"a" से एक file open करना अंत में नया text add करता है और old content रखता है। file:seek(whence, offset) position move करता है, जहाँ whence "set" शुरुआत के लिए, "cur" current position के लिए, और "end" अंत के लिए है, और यह नई position return करता है। seek("end") file size ढूंढने का एक quick तरीका है।

Note: बिना arguments के file:seek() बिना move हुए current position return करता है।

उदाहरण: Appending and seeking

lua
local path = "/tmp/lua_demo_append.txt"
local f = assert(io.open(path, "w"))
f:write("abc\n")
f:close()

f = assert(io.open(path, "a"))
f:write("def\n")
f:close()

f = assert(io.open(path, "r"))
print(f:seek("end"))
f:seek("set", 4)
print(f:read("l"))
print(f:seek())
f:close()

-- Output:
-- 8
-- def
-- 8
{# 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. "a" की बजाय "w" से open करना और file wipe कर देना
  2. whence और offset को गलत order में रखना
  3. यह भूल जाना कि seek zero से bytes count करता है
चैप्टर सारांश
  • "a" अंत में append करता है
  • seek("set", n) शुरुआत से jump करता है
  • seek("end") size return करता है
  • seek() current position return करता है
🔒

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.