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

[Solved] fs API seems broken, but maybe it's just me

Started by RaiuPlays, 20 October 2016 - 12:10 AM
RaiuPlays #1
Posted 20 October 2016 - 02:10 AM
Save function: http://pastebin.com/mdud8uTW
Whole program: http://pastebin.com/73wJD4ra
I'm trying to make a function that has the program rewrite itself to save information, in this case I'm saving it to a separate file for testing purposes. I've made something like this before that worked perfectly fine, but now it's being weird. Here's the rundown of what happens:
  1. The programs reads its own code which is kept as a string in the 'content' variable.
  2. It deletes the information about the 'game' variable from 'content' in order to write new information about it.
  3. It writes the new data from the 'game' table, which takes 6 lines if it's empty.
  4. Then it's supposed to write the remaining code from 'content'.
  5. What actually happens is it deletes all the contents in the 'testfile' when it writes the 24th or 25th line (depending on how many lines it wrote from 'game'), including what it already wrote to the file.
  6. I've confirmed that the information in 'content' exists, it just deletes everything in the file after it writes a certain number of lines.
  7. All the commented out stuff is just from me trying to figure this out.
  8. This glitch doesn't seem to happen if I have it write 'game' by itself. This is after testing it with 30 items in 'game.name' (it automatically writes one line per item), but for some reason it doesn't want to work with the rest of it.
To reiterate, after it writes a certain number of lines, everything, including what it already wrote, gets deleted except for the file itself. It leaves me with just an empty file. I've tried reinstalling both Minecraft and ComputerCraft which fixes it, until I restart the game, then this starts happening again. I've tried using different versions of ComputerCraft and Forge, still happens. I don't have any other mods installed. I've tried creating a new world in-game, still happens. I've tried rebooting my actual physical computer, still happens. I've checked and rewritten my own code multiple times, and as far as I know everything should be fine. If anyone can help me figure out why this is happening, I'd be much obliged.

Edit: This is what the save function originally looked like:
Spoilerfunction saveData()
local file = fs.open("speedrun", "r")
local content = file.readAll()
file.close()
local a = string.find(content, "local listStart")
content = string.sub(content, a)
local file = fs.open("testfile", "w")
file.writeLine("local game = "..textutils.serialize(game))
file.write(content)
file.close()
end
– The load function did not exist at this point, nor was it necessary for anything other than testing.
I should also point out that I don't get an error. The program functions perfectly fine. It just doesn't write to files properly. It's exactly what I explained above, no more, no less.
Edited on 20 October 2016 - 02:54 AM
KingofGamesYami #2
Posted 20 October 2016 - 02:27 AM
You're trying to load your data (in the loadData function) using textutils.unserialize. That won't work, because you are writing more than just a table to the file.

Try removing lines 37 through 39.
RaiuPlays #3
Posted 20 October 2016 - 02:33 AM
The loadData function was made during my testing before I made those lines you suggested. It's not actually doing anything.
RaiuPlays #4
Posted 20 October 2016 - 02:38 AM
I made an extra note to clear up what my code was suppose to be.
Bomb Bloke #5
Posted 20 October 2016 - 04:29 AM
The only obvious problem I can see is line 86 in this paste.

How about we simplify a bit, then work from there?

function saveData()
	local input, output, line = fs.open("speedrun", "r"), fs.open("testfile", "w")
	
	output.writeLine( "local game = { name = {}, levels = { {}, }, }" )
	
	repeat line = input.readLine() until line == "local listStart = 1"
	
	repeat
		output.writeLine(line)
		line = input.readLine()
	until not line
	
	input.close()
	output.close()
end

Edit:

You're not running into the free space limit, are you?
Edited on 20 October 2016 - 02:33 AM
RaiuPlays #6
Posted 20 October 2016 - 04:54 AM
The only obvious problem I can see is line 86 in this paste.

How about we simplify a bit, then work from there?

function saveData()
	local input, output, line = fs.open("speedrun", "r"), fs.open("testfile", "w")
	
	output.writeLine( "local game = { name = {}, levels = { {}, }, }" )
	
	repeat line = input.readLine() until line == "local listStart = 1"
	
	repeat
		output.writeLine(line)
		line = input.readLine()
	until not line
	
	input.close()
	output.close()
end

Edit:

You're not running into the free space limit, are you?
So first of all, that's a really nice approach which I hadn't thought of so thanks for that suggestion. Second, yes, I ran out of space on that computer. I completely forgot that was a thing. I was keeping some non-computercraft files in there that didn't need to be there. Thanks so much, everything is working now.