Welcome, Guest. Please Login or Register.
eccoMAGIC Forums
07/21/24 at 21:21:37
Home Help Search Login Register


Pages: 1
Send Topic Print
  Rating
Rate:
Rating:
Rating: 0.00
Votes: 0

5
4
3
2
1












5
4
3
2
1


1


2


3


4


5


6


7


8


9


10

LUA files and such.. (Popularity: 3176 )
Admin
Administrator
*****


I love Ecco!

Posts: 134
Show the link to this post LUA files and such..
11/18/07 at 19:05:09
 

Albert asks the correct question Quote:

--- In ecco_pro@yahoogroups.com, "albertschepers" <as@...> wrote:
>
> Attempting to create a text file of data from the ecco file.
>
> 1 NumberItems = get_select_items()
> 2 MaxItems = table.maxn(NumberItems)
> 3 msgbox(MaxItems .. " items selected","")
> 4 if MaxItems >0 then
> 5 filename = "c:\\test.txt"
> 6 io.open(filename, "w+")
> 7 for i=1,MaxItems do
> 8 y=tostring(i)..", "..get_item_text(NumberItems[i]).."\n"
> 9 msgbox(y,"")
> 10 io.write(y)
> 11 end
> 12 io.close()
> 13 else
> 14 msgbox("no selected items","")
> 15 end
>
>
> This is the code that I am using. Please note the mix of LUA
> functions and eccoext functions. It works fine up to line 10 when I
> get the message "standard output file is closed". This does not make
> sense.


 
 
and the answer is,
 
something like this:
 
 

 
There are actually 2 'systems' of file operations in LUA.
 
 
The first one is simple, and most likely a good solution for most...
 
 
Quote:
The simple way does all of its operations on two current files. The library initializes the current input file as the process's standard input (stdin) and the current output file as the process's standard output (stdout), usually a screen output, or invisible in LUA within ecco.

We can change those current files with the io.input and io.output functions. A call like io.input(filename) opens the given file (in read mode) and sets it as the current input file. From this point on, all input will come from this file, until another call to io.input; io.output does a similar job for output. In case of errors, both functions raise the error. If you want to handle errors directly, you must use io.open, from the complex system (see below).

 
 
 
so... simply change the io.open to io.output... and you're all set!
 
 
 
 
or... use the io.open, but within the complex system,
 
something like this:
 
 
Quote:

For more control over I/O, you can use the complete system. A central concept in this model is the file handle, which is equivalent to streams (FILE*) in C: It represents an open file with a current position. The advantage here is specific operations are charged against/under a specific file, based on the handle.

For example, o open a file, you use the io.open function, which mimics the fopen function in C. It receives as arguments the name of the file to open plus a mode string. That mode string may contain an `r for reading, a `w for writing (which also erases any previous content of the file), or an `a for appending, plus an optional `b to open binary files. The open function returns a new handle for the file. In case of errors, open returns nil, plus an error message and an error number.


For example:

local f = assert(io.open(filename, "r"))
local t = f:read("*all")
f:close()



note: We can mix the complete system with the simple system. We get the current output file handle by calling io.output(), without arguments. We set the current input file handle with the call io.input(handle).



 
 
example code in LUA:
 
Code:

    local inp = assert(io.open(arg[1], "rb"))
    local out = assert(io.open(arg[2], "wb"))
	
    local data = inp:read("*all")
    data = string.gsub(data, "\r\n", "\n")
    out:write(data)
	
    assert(out:close())

 


 
 
and there you have it!
 
Back to top
 
 

The Administrator.
WWW   IP Logged
Pages: 1
Send Topic Print