Advertisement
kijato

lua_open_file

Apr 30th, 2020
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. --
  2. -- http://nyelvek.inf.elte.hu/leirasok/Lua/index.php?chapter=12#section_6
  3. --
  4.  
  5. print( arg[0] )
  6.  
  7. filename = "itr_ascii_minta.dat"
  8.  
  9. -- Opens a file in read
  10. file = io.open(filename, "r")
  11.  
  12. -- prints the first line of the file
  13. print("¤"..file:read()..\n")
  14.  
  15. -- ...It offsets the cursor from the 25 positions prior to the end of file. The read function prints remainder of the file from seek position.
  16. file:seek("end",-25)
  17. print("×"..file:read("*a")..\n")
  18.  
  19. file:seek("set")
  20. wholeFile = file:read("*a")
  21. print(wholeFile)
  22.  
  23.  
  24. --[[
  25. Source:
  26.     301  Pontok
  27.     310   "pontszám szöveggel"       95.530      283.150     0.000         4315    0    0
  28.     301  Vonalak
  29.     301  Feliratok
  30.     340      150.400      170.250     0.0      1     10   100   4   0   0   0 szöveg szóközzel
  31.     341      253.470      214.660     0.0      1    105     1   1   1   0   0   0.0  1.00   1     1309      855 szöveg szóközzel2
  32.     301  Jelkulcsok
  33.     301  Ívek
  34. Destination
  35.     '301' = {"Pontok", "Vonalak", "Feliratok", "Jelkulcsok", "Ívek"}
  36.     '310' = {" "pontszám szöveggel"       95.530      283.150     0.000         4315    0    0"}
  37.     ...
  38. ]]
  39. t = {}
  40. file:seek("set")
  41. for line in file:lines() do
  42.     print(line)
  43.     for k, v in string.gfind(line, "(%a+) (.+)") do
  44.         t[k] = v
  45.     end
  46. end
  47.  
  48. -- closes the opened file
  49. file:close()
  50.  
  51.  
  52.  
  53.  
  54.  
  55. t = {}
  56. s = "from=world, to=Lua"
  57. for k, v in string.gmatch(s, "(%w+)=(%w+)") do
  58.   t[k] = v
  59. end
  60. for k, v in pairs(t) do
  61.   print(k, v)
  62. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement