(scripts/11 - FileOutput.lua)
01:function hexView( szFileName ) 02: local f = assert( io.open( szFileName, "rb" ) ) 03: local block = 10 04: while true do 05: local strResult = "" 06: local bytes = f:read(block) 07: if not bytes then break end 08: for b in string.gfind(bytes, ".") do 09: strResult = strResult .. ( string.format("%02X", string.byte( b ) ) ) 10: end 11: strResult = strResult .. ( string.rep( " ", block - string.len( bytes ) ) ) .. " " 12: strResult = strResult .. string.gsub( bytes, "%c", "." ) 13: print( strResult ) 14: end 15: f:close() 16:end 17: 18:SCREEN_BOTTOM = 0 19:BGBotText = Screen.LoadTextBG() 20:Screen.Initialize( SCREEN_BOTTOM, BGBotText ) 21: 22:fileName = "/scripts/test.dat" 23:fout = io.open( fileName, "wt" ) 24:if not fout then 25: print( "Failed to open: " .. fileName ) 26:else 27: print( "File opened successfully..." ) 28: fout:write( string.format( "%f %f %f", 6.0, -3.23, 15e12 ) ) 29: fout:write( "Time: " .. ( 2000 + DSLua.Year() ) .. "/" .. DSLua.Month() .. "/" .. DSLua.Day() .. " " .. DSLua.Hour() .. ":" .. DSLua.Min() .. ":" .. DSLua.Sec() ) 30: fout:write( "Pi=", 3.1415, " ten=", 10, "." ) 31: fout:write( 1, 2, 3, " finished" ) 32: 33: if not ( fout:close() ) then 34: print( "Failed to close file" ) 35: else 36: print( "Write complete\n" ) 37: fout = nil 38: hexView( fileName ) 39: end 40:end 41: 42:print( "Press any button to cont..." ) 43:DSLua.WaitForAnyKey() 44:print() 45: 46:fin = io.open( fileName, "rb" ) 47:if not fin then 48: print( "Failed to open: " .. fileName ) 49:else 50: print( "File ready to read..." ) 51: local n1, n2, n3 = fin:read( "*number", "*number", "*number" ) 52: print( "read in [" .. n1 .."][" .. n2 .."][" .. n3 .. "]" ) 53: fin:close() 54:end 55: 56:print( "Press START to exit..." ) 57:repeat 58: Screen.WaitForVBL() 59:until Pads.Start()
プログラムの解説
01:function hexView( szFileName ) 02: local f = assert( io.open( szFileName, "rb" ) ) 03: local block = 10 04: while true do 05: local strResult = "" 06: local bytes = f:read(block) 07: if not bytes then break end 08: for b in string.gfind(bytes, ".") do 09: strResult = strResult .. ( string.format("%02X", string.byte( b ) ) ) 10: end 11: strResult = strResult .. ( string.rep( " ", block - string.len( bytes ) ) ) .. " " 12: strResult = strResult .. string.gsub( bytes, "%c", "." ) 13: print( strResult ) 14: end 15: f:close() 16:end 17: 18:SCREEN_BOTTOM = 0 19:BGBotText = Screen.LoadTextBG() 20:Screen.Initialize( SCREEN_BOTTOM, BGBotText ) 21: 22:fileName = "/scripts/test.dat" 23:fout = io.open( fileName, "wt" ) 24:if not fout then 25: print( "Failed to open: " .. fileName ) 26:else 27: print( "File opened successfully..." ) 28: fout:write( string.format( "%f %f %f", 6.0, -3.23, 15e12 ) ) 29: fout:write( "Time: " .. ( 2000 + DSLua.Year() ) .. "/" .. DSLua.Month() .. "/" .. DSLua.Day() .. " " .. DSLua.Hour() .. ":" .. DSLua.Min() .. ":" .. DSLua.Sec() ) 30: fout:write( "Pi=", 3.1415, " ten=", 10, "." ) 31: fout:write( 1, 2, 3, " finished" ) 32: 33: if not ( fout:close() ) then 34: print( "Failed to close file" ) 35: else 36: print( "Write complete\n" ) 37: fout = nil 38: hexView( fileName ) 39: end 40:end 41: 42:print( "Press any button to cont..." ) 43:DSLua.WaitForAnyKey() 44:print() 45: 46:fin = io.open( fileName, "rb" ) 47:if not fin then 48: print( "Failed to open: " .. fileName ) 49:else 50: print( "File ready to read..." ) 51: local n1, n2, n3 = fin:read( "*number", "*number", "*number" ) 52: print( "read in [" .. n1 .."][" .. n2 .."][" .. n3 .. "]" ) 53: fin:close() 54:end 55: 56:print( "Press START to exit..." ) 57:repeat 58: Screen.WaitForVBL() 59:until Pads.Start()