Lua Script error: bad argument #2 'to open' (invalid mode) - file

I am trying to create a file or read a already existing file but I keep getting the same error: bad argument #2 'to open' (invalid mode)
player_pos = player.pposition()
world_nam = server.worldName()
attribs = player.attributes()
xp = attribs.id(6).value
timesavename = world_nam .. player_pos .. xp
f = io.open(timesavename, ".txt", "r")
if f ~= nil then
io.input(f)
resultstart = f:read("*line")
resultstop = f:read("*line")
f:close()
end

Using , in a function will make it think its another argument, like in the comment you got you should concat the strings with ..

Related

What does "no error" mean when writing to a file in Lua?

I am writing a script to add one line in a .txt per video while using MPV.
However, I am getting a weird error on line 68 with the for loop.
It merely tells me: no error. If I add an error parameter to file:write(message, error), it gives me another error message, stating: bad argument #2 to 'write' (string expected, got function). Any help would be appreciated.
function on_file_end(event)
if not paused then totaltime = totaltime + os.clock() - lasttime end
local message = (totaltime .. "s, " .. timeloaded .. ", " .. filename)
local lines = {}
local file = io.open(logpath, "r+")
if file_exists(logpath) then
for l in file:lines() do
if not l:find(message, 1, true) then
lines[#lines+1] = 1
file:write(message)
end
end
file:close()
end
end
bad argument #2 to 'write' (string expected, got function)
error is not an "error parameter" it is a global function that allows to raise your own errors in Lua.
See https://www.lua.org/manual/5.4/manual.html#pdf-error

dispose syntax error when using fortran open statement

I am opening a file which has to be deleted at the end. The following command complains about using dispose.
f = "espy.tmp"; h = "formatted"; r = "read"
Open (newunit=u, file=f, form=h, action=r, &
status="old", dispose="delete")
lib/core.f:177:21:
status="old", dispose="delete")
1
Error: Syntax error in OPEN statement at (1)
Dispose is a non-standard compiler extension (and not supported by your compiler). As described in this answer, the standard way to do this is to delete the file on closure:
f = "espy.tmp"; h = "formatted"; r = "read"
Open (newunit=u, file=f, form=h, action=r, &
status="old")
close(u, status='delete')
Or, you could use temporary/scratch files (no filename):
f = "espy.tmp"; h = "formatted"; r = "read"
Open (newunit=u, form=h, action=r, &
status="old", status='scratch')

open(create) non existing file in lua

I'm trying to save a configtable(parsed to string) to a file wich is created just in time.
local cfg_string = table.tostring(cfg_table)
local file_name = ""
local cfg_file = ""
file_name = com:line(nil) -- reads a line of input from user via terminal
file_name = string.format("some_prefix-%s-some_suffix.lua",file_name)
-- file does NOT exist at this line
cfg_file = io.open("/dir/subdir/"..file_name,"w")
-- file now should exist
os.syslog(type(file_name)) -> string
os.syslog(type(cfg_file)) -> nil
os.syslog(type(cfg_string)) -> string
cfg_file:write(cfg_string)
cfg_file:write(cfg_string) throws "attempt to call "write" a nil value".
So hu,.. cfg_file is nil I know, but why? I also tried to io.open() with "a" flag, but this doesn't work too. The directory exists!
Thanks for your help.
Actually, my code is working. The error is thrown because missig write-permission to dir.
Everyone with such Errors should try
handle_name, err = io.open(file,"w")
print(err)

Passing na.strings to dbWriteTable

I'm trying to run the following command in R in order to read a local tab-delimited file as a SQLite database:
library(RSQLite)
banco <- dbConnect(drv = "SQLite",
dbname = "data.sqlite")
dbWriteTable(conn = banco,
name = "Tarefas",
value = "data.tsv",
sep = "\t",
dec = ",",
na.strings = c("", NA),
row.names = FALSE,
header = TRUE)
However, the statements above yield the following error:
Error in read.table(fn, sep = sep, header = header, skip = skip, nrows
= nrows, : formal argument "na.strings" matched by multiple actual arguments
Which makes me think I'm not being able to pass na.strings explicitly as a read.delim argument. Running dbWriteTable without this argument gives me "RS-DBI driver: (RS_sqlite_import: ./data.tsv line 17696 expected 20 columns of data but found 18)". This is understandable, since I've checked line 17696 and it is almost completely blank.
Another test run using sqldf also gives me an error:
> read.csv2.sql(file = "data.tsv",
+ sql = "CREATE TABLE Tarefas AS SELECT * FROM FILE LIMIT 5",
+ dbname = "data.sqlite",
+ header = TRUE,
+ row.names = FALSE)
Error in sqliteExecStatement(con, statement, bind.data) :
RS-DBI driver: (error in statement: no such table: FILE)
Which I believe is an unrelated error, but still very confusing for someone who's pretty much an absolute SQL noob such as myself. Runnin read.csv.sql instead gives me this error:
Error in read.table(fn, sep = sep, header = header, skip = skip, nrows = nrows, :
more columns than column names
So is there a way to pass na.strings = c("", NA) at dbWriteTable? Is there a better way to read 10 GB tab-delimited files into R aside from sqldf and RSQLite? I've already tried data.table and ff.

issue using wavread in matlab

I have the this:
name = ['Anca', 'Bogdan', 'Francois', 'Jerome', 'Simina'];
for i=1:size(name,1)
temp = name(i,:);
tempwav = wavread(temp);
end
And I get this error:
Error in Load_data (line 7)
tempwav = wavread(temp);
meaning this line: tempwav = wavread(temp);
The .wav files are there I just don't know what is the problem. Pls help
Your way of creating the variable name will result in the value AncaBogdanFrancoisJeromeSimina. Instead you should use a cell array (note the curly brackets)
name = {'Anca', 'Bogdan', 'Francois', 'Jerome', 'Simina'};
for i=1:length(name)
temp = name{i};
tempwav = wavread(temp);
end

Resources