Total Pageviews

Thursday, April 3, 2014

IO.Open របស់ភាសាឡូអា

Syntax: io.open (filename_path, [, mode] )

Filename_path(តំរូវការ) វាជា string ឈ្មោះរបស់ហ្វាល់ជាមួយនឹង Path ដែលត្រូវបើក។ សូមប្រើ system.pathForFile() ក្នុងការបង្កើត ឈ្មោះរបស់ហ្វាល់ ហើយនឹង Path ទៅកាន់ system.ResourceDirector, system.DocumentsDirectory, system.TemporaryDirectory Or system,CachesDirectory
Mode(ជំរើស) សំរាប់ String ប្រើក្នុងការកំនត់ហ្វាល់ ម៉ូដដើម្បីបើកវា។ ម៉ូដរបស់វាអាចមានះ
“r” សំរាប់អានតែប៉ុណ្ណោះ (ហ្វាល់ដើម)​ ហ្វាល់នឹងចាប់ផ្តើម pointer ដែលដាក់នៅចាប់ផ្តើមនៃហ្វាល់
“w”​ សំរាប់សរសេរតែប៉ុណ្ណោះ នឹងសរសេរ over បើសិនជាមានហ្វាល់ តែបើគ្មាននឹងបង្កើត
“a”append mode (សរសេរ) ដែលទិន្នន័យមុនត្រូវបានថែរក្សាទុក
“r+” សំរាប់ អាប់ដេត ម៉ូដ(read/write) ហើយថែរក្សាហ្វាល់ដើម
“w+” អាបដេតម៉ូត (read/write) ដែលទិន្នន័យមុនៗត្រូវបានលុប
“a+” append update mode (read/write) ទិន្នន័យដើមត្រូវបានថែរក្សា ហើយសរសេរតែ នៅខាងចុងហ្វាល់
សូមមើលកូដះ
local path = system.pathForFile( "data.txt", system.DocumentsDirectory )
-- io.open opens a file at path. returns nil if no file found
local fileHandle, errorString = io.open( path, "r" )
if fileHandle then
   -- read all contents of file into a string
   local contents = fileHandle:read( "*a" )
   print( "Contents of " .. path .. "\n" .. contents )
else
   print( "Reason open failed: " .. errorString )  -- display failure message in terminal
   -- create file because it doesn't exist yet
   fileHandle = io.open( path, "w" )
   if fileHandle then
        print( "Created file" )
   else
        print( "Create file failed!" )
   end
   local numbers = {1,2,3,4,5,6,7,8,9}
   fileHandle:write( "Feed me data!\n", numbers[1], numbers[2], "\n" )
   for _,v in ipairs( numbers ) do
       fileHandle:write( v, " " )
   end
   fileHandle:write( "\nNo more data\n" )
end

io.close( fileHandle )

No comments:

Post a Comment