lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Nov 28, 2011 at 8:04 AM, santhosh reddy
<santhoshreddy539@gmail.com> wrote:
> Can any one please help me out in reading a file character by character and
> printing each character to the terminal.

If we had a file called test.txt with contents 'hello\n' then you can
read a 'character' as a string of length one:

D:\test>lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio
> f = io.open('test.txt')
> = f:read(1)
h
> = f:read(1)
e
> = f:read(1)
l
> = f:read(1)
l
> = f:read(1)
o
> = f:read(1)


> = f:read(1)
nil

When you get nil from f.read, then you know you're finished.

steve d.