IO.FileSystem
POINT
CRT stdio を拡張したもの
fp = io.input("text");
io.read(); -- EOF -> nil
io.input(); -- console に戻す.
io.output("text");
-- \n ふくまず
io.write();
-- console に戻す
io.output();
-- file handle 経由
do
local file = io.open("d:/lua/test.txt", "r");
if ( file ) then
-- filehandle:read( format );
-- [ *l | *a | *n ]
local t = file:read("*a");
print(t);
else
print("no file");
end
end
-- file handle 経由
local fp = io.open("d:/lua/test.txt", "w");
if ( fp ) then
-- fp:read( format );
-- [ *l | *a | *n ]
local t = fp:read("*a");
print(t);
else
print("no file");
end
WARNING
当然ながら文字コードが SHIFT_JIS や UTF-8 は問題ないが UNICODE はうまく動かない
POINT
file 入出力 library も oo を利用する
( filehandle は userdata )
fp = io.open( file, "r" );
line = fp:read();
f = io.open( "test.txt", "r")
out = io.open("out.txt","w")
-- 各行を処理をする
for line in f:lines() do
out:write(line.."\n")
print(line)
end
out:close()
f:close()