Appending और Seeking
Append mode अंत में add करता है, और seek read या write position move करता है।
In this page:
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
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
Login to try C/C++/Java/PHP code in the editor
आम गलतियां
- "a" की बजाय "w" से open करना और file wipe कर देना
- whence और offset को गलत order में रखना
- यह भूल जाना कि 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: