6 posts
Location
where am i
Posted 23 January 2015 - 02:45 AM
Hi, I made some code that takes a file copies it, pastes in new code at the top, and then adds in the old code. Right now I can add in 1 line of code since I don't know how to make a new line in a string. What I mean is how would does someone have a string that in the file is 1 line, but when written to a file is 2 lines. I believe it's something like
/n which I tried in my
emulator and it gets very angry:
The code for my program is here:
local newCode = "abc"
----------------------------
local f = fs.open(shell.getRunningProgram(), "r")--Retrieves files code. I used this program to test.
local oldFile = f.readAll()
f.close()
local f = fs.open(shell.getRunningProgram(), "w")--Writes over old code.
f.write(newCode)
f.close()
local f = fs.open(shell.getRunningProgram(), "a")--Adds original code to the newly added code
f.write(oldFile)
f.close()
----------------------------
The name of the program is filejump btw.
Anyways, thanks for reading and hopefully you can help me!
Edited on 23 January 2015 - 01:46 AM
113 posts
Location
This page
Posted 23 January 2015 - 03:12 AM
Well I just tested this out in minecraft itself and the code worked fine.
Now if you are wondering how to get a new line it is '\n'
When I tried your code I changed the newCode to equal abc\n and it created a new line, however you could also do fs.writeLine(newCode) which will write the text then append a new line.
I edited the code and tried it and this worked the way I believe you wanted it to work.
local newCode = "abc"
local editFile=shell.getRunningProgram()
----------------------------
local f = fs.open(editFile, "r")--Retrieves files code. I used this program to test.
local oldFile = f.readAll()
f.close()
local f = fs.open(editFile, "w")--Writes over old code.
f.writeLine(newCode)
f.write(oldFile)
f.close()
----------------------------
Also I don't know why you are getting a Java exception error, but I believe it is the emulator, because as I said it worked fine for me in minecraft.
Edited on 23 January 2015 - 02:24 AM
7083 posts
Location
Tasmania (AU)
Posted 23 January 2015 - 04:50 AM
Also I don't know why you are getting a Java exception error, but I believe it is the emulator, because as I said it worked fine for me in minecraft.
Just a guess, but likely the emulator in concern is failing to drop the file handle on the script that's running at the correct time (ie, after loading it from the disk, but before actually executing it).
6 posts
Location
where am i
Posted 23 January 2015 - 05:21 AM
Awesome! Your code looks cool and at least 1/4 smaller than before. As for the Java error I don't even really know, honestly I think I put the \n some weird way other so who knows
Anyways thanks for the help!
Edited on 13 September 2017 - 10:30 PM
496 posts
Location
New Zealand
Posted 25 January 2015 - 03:27 AM
Also I don't know why you are getting a Java exception error, but I believe it is the emulator, because as I said it worked fine for me in minecraft.
Just a guess, but likely the emulator in concern is failing to drop the file handle on the script that's running at the correct time (ie, after loading it from the disk, but before actually executing it).
CCEmuRedux uses ComputerCraft, and doesn't touch script handling, so if it was a lingering file handle it would be either CC or Java being dumb.
I've tried running code that edits the running program file in CCEmuRedux and it's worked every time with no exceptions.
If OP is able to replicate the issue, could you please post the output from running the emulator from the command line, when the exception occurs.