Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Writing to file — Gideros Forum

Writing to file

ar2rsawseenar2rsawseen Maintainer
edited January 2012 in General questions
Hi,

I've tried using Save & load a table script but it didn't work, I've narrowed it down to this simple snippet which doesn't work:

file = io.open( "test.txt", "w" )
file:write("somestring")
io.close(file)

Basically anything I save to file is not saved there, even if file is not created, is created, is created and added to Gideros project. Do I miss something? Any permisions?

I'm running Windows 7 32bit with Gideros v2011.9 beta3

P.S Reading from files works

Comments

  • Try this:
    f= io.open( "test.txt", "w" )
    file:write("somestring")
    io.close(f)
    There are actually some file:xxxxx commands in LUA.

    http://www.lua.org/manual/5.1/
  • Ok, I read more in the net, and I am not so sure if that is right what I wrote. Atilim??
  • Specify the document folder with the file name. I think the resouce folder is used by default. There you can not write to.
  • What do you mean by specify document folder?
    How can I do that and would this folder be visible in mobile phone?

    BTW: How can I style code when posting it here?
  • I might be wrong, but it seems that file is same file handle returned by io object. meaning, they just used variable name file in LUA documentation, it's not an object by itself. But I might be wrong.
  • atilimatilim Maintainer
    edited January 2012
    Hi all,

    There are two types of file access in Lua. "Simple" and "Complete" I/O model. Complete I/O model have more control over I/O and also it's simple to use. Therefore, you can simply omit Simple I/O model and directly use Complete I/O model always. Here is a simple example of Complete I/O model:
    local file1 = io.open("|D|text.txt", "w")  --> open text.txt in documents directory to write
    file1:write("somestring")                  --> write a string
    file1:close()                              --> close the file
     
    ---
     
    local file2 = io.open("|D|text.txt", "r")  --> open text.txt in documents directory to read
    local str = file2:read("*all")             --> read whole file
    print(str)                                 --> print the contents
    file2:close()                              --> close the file
    You can read more here: http://www.lua.org/pil/21.html
    Here is the documentation about file system of gideros: http://www.giderosmobile.com/documentation/file_system.html (but the examples in this document use Simple I/O model)

    You can write your code inside this:
    <pre lang="lua">
    </pre>


    +1 -1 (+2 / -0 )Share on Facebook
  • how to read file line by line?
  • if you have my book "Learn Lua for iOS Game Development" you can see Chapter #3 that deals with file operations and on page 30 is the answer you are looking for
    for line in io.lines(filename) do 
      print(line)
    end
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
Sign In or Register to comment.