This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
jrbc1's profile picture

API help!

Started by jrbc1, 19 March 2012 - 10:25 PM
jrbc1 #1
Posted 19 March 2012 - 11:25 PM
I am creating an API that has only one function that saves a message of 18 lines, but I get this error when testing:

bios:278: bad argument: string expected, got table

The test file:

os.unloadAPI(gapi)
os.loadAPI(gapi)
gapi.newfile()

The code:


local line1
local line2
local line3
local line4
local line5
local line6
local line7
local line8
local line9
local line10
local line11
local line12
local line13
local line14
local line15
local line16
local line17
local line18
local s
local name
function getVersion()
	return 0.1
end
function newfile()
	print("File creator")
	line1 = read()
line2 = read()
line3 = read()
line4 = read()
line5 = read()
line6 = read()
line7 = read()
line8 = read()
line9 = read()
line10 = read()
line11 = read()
line12 = read()
line13 = read()
line14 = read()
line15 = read()
line16 = read()
line17 = read()
line18 = read()
print("------------------------")
print("Name of file:")
name = read()
s = fs.open(name, "w")
s.writeLine(line1)
s.writeLine(line2)
s.writeLine(line3)
s.writeLine(line4)
s.writeLine(line5)
s.writeLine(line6)
s.writeLine(line7)
s.writeLine(line8)
s.writeLine(line9)
s.writeLine(line10)
s.writeLine(line11)
s.writeLine(line12)
s.writeLine(line13)
s.writeLine(line14)
s.writeLine(line15)
s.writeLine(line16)
s.writeLine(line17)
s.writeLine(line18)
print("Saved!")
end

If you can, responds in Spanish please!
Espen #2
Posted 19 March 2012 - 11:36 PM
os.loadAPI() takes a String with the filename as an argument.
os.unloadAPI() takes a table.

I.e. add double-quotes to your os.loadAPI() call.

EDIT:
Also instead of creating 18 similar variables, I'd recommend this:

local line = {}

for i = 1, 18 do
  line[i] = read()
end

for i = 1, #line do
  write( line[i] )
end
Edited on 19 March 2012 - 10:40 PM
jrbc1 #3
Posted 19 March 2012 - 11:45 PM
os.loadAPI() takes a String with the filename as an argument.
os.unloadAPI() takes a table.

I.e. add double-quotes to your os.loadAPI() call.

EDIT:
Also instead of creating 18 similar variables, I'd recommend this:

local line = {}

for i = 1, 18 do
  line[i] = read()
end

for i = 1, #line do
  write( line[i] )
end

Now is not loading gapi.newfile()
gapi:3: attempt to index ? (a nil value)

I edited the API file and now is like this:



local line
local lines
local s
local name
function getVersion()
		return 0.1
end
function newfile(lines)
		print("File creator")
		for i = 1, lines do
				line[i] = read()
		end
		print("------------------------")
		print("Name of file:")
		name = read()
		s = fs.open(name, "w")
		for i = 1, lines do
				s.writeLine(line[i])
		end
		print("Saved!")
end
Espen #4
Posted 20 March 2012 - 12:25 AM
  • 'line' has to be initialized as a table.
  • The for-loop to write the lines to file should run until #line, so that you don't have to make changes in two places. (The # infront means you'll get the length/size of the table, string, etc.)
  • I'd personally suggest using io.open() instead of fs.open(), since the latter has some problems (they have been addressed somewhere though, but I can't remember atm.).
  • Also it's good advice to close the file after all opartions on it are done.
Here a working version with the changes:
GAPI:

local line = {}
local s
local name

function getVersion()
  return 0.1
end

function newfile()
  print("File creator")
  for i = 1, 18 do
	line[i] = read()
  end

  print("------------------------")
  print("Name of file:")
  name = read()

  s = io.open(name, "w")
  for i = 1, #line do
	s:write(line[i].."n")
  end
  s:close()
  print("Saved!")
end

Test-Code:

os.loadAPI( "gapi" )
if not gapi then
  error( "GAPI not loaded!" )
end

gapi.newfile()

os.unloadAPI( gapi )
Edited on 20 March 2012 - 12:02 AM
jrbc1 #5
Posted 20 March 2012 - 12:56 AM
  • 'line' has to be initialized as a table.
  • The for-loop to write the lines to file should run until #line, so that you don't have to make changes in two places. (The # infront means you'll get the length/size of the table, string, etc.)
  • I'd personally suggest using io.open() instead of fs.open(), since the latter has some problems (they have been addressed somewhere though, but I can't remember atm.).
  • Also it's good advice to close the file after all opartions on it are done.
Here a working version with the changes:
GAPI:

local line = {}
local s
local name

function getVersion()
  return 0.1
end

function newfile()
  print("File creator")
  for i = 1, 18 do
	line[i] = read()
  end

  print("------------------------")
  print("Name of file:")
  name = read()

  s = io.open(name, "w")
  for i = 1, #line do
	s:write(line[i].."n")
  end
  s:close()
  print("Saved!")
end

newfile()

Test-Code:

os.loadAPI( "gapi" )
if not gapi then
  error( "GAPI not loaded!" )
end

gapi.newfile()

os.unloadAPI( gapi )

The error:
gapi:3:GAPI not loaded!
Espen #6
Posted 20 March 2012 - 01:06 AM
Did you actually save the API file as "gapi" or did you use another name?
And did you store it in the root directory?

Also, I removed "newfile()" from the end of the API, which I forgot to take out. I updated my previous post.
But this has nothing to do with your new error, I promise.

Just save the API as "gapi" in / and create another file at the same location for the test code.
Then run the test code. It should definitely work, because it did for me and I'm not using magic. :D/>/>
jrbc1 #7
Posted 20 March 2012 - 01:22 AM
Did you actually save the API file as "gapi" or did you use another name?
And did you store it in the root directory?

Also, I removed "newfile()" from the end of the API, which I forgot to take out. I updated my previous post.
But this has nothing to do with your new error, I promise.

Just save the API as "gapi" in / and create another file at the same location for the test code.
Then run the test code. It should definitely work, because it did for me and I'm not using magic. :D/>/>

Thank you, newfile() now works
If i need more help, i ask here