I'm trying to write a serialized string to a file but I want to write /n to the file instead of starting a new line
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Writting Strings
Started by je06, 20 July 2015 - 04:22 PMPosted 20 July 2015 - 06:22 PM
I've done some searching but I can't find the answer to my problem.
I'm trying to write a serialized string to a file but I want to write /n to the file instead of starting a new line
I'm trying to write a serialized string to a file but I want to write /n to the file instead of starting a new line
Posted 20 July 2015 - 06:27 PM
I'm assuming you mean \n, and \n is a newline character, so it will always start a new line.
You could write \\n, which would display as \n, but not insert a newline.
You could write \\n, which would display as \n, but not insert a newline.
Posted 20 July 2015 - 07:06 PM
Thanks
Posted 20 July 2015 - 07:12 PM
I'm writing a program that stores multiple files in one then then exports them when you run the one file but I'm having trouble getting it to work. It should read the files, serialize them, and then export them in a variable called code but it's not.
I put a print function in the code to make sure the getData function works and it does.
My Code:http://pastebin.com/bQ4YSG3w
I put a print function in the code to make sure the getData function works and it does.
My Code:http://pastebin.com/bQ4YSG3w
Posted 20 July 2015 - 07:38 PM
Threads merged. Please stick to one topic for all questions about a given piece of code.
Posted 20 July 2015 - 07:56 PM
So its a self extracting archive?
Personally, I'd say its more trouble than its worth, I tried this myself, and found how difficult it can be.
I could probably do it now that I know a bit more, but there are mutliple versions of this by several poeple already,
so I'd just use one of them, or modify one to fit your needs.
I believe you need to convert each file to base64 or something. Can't really give you too many details.
Here is a working one:
http://www.computerc...le-permissions/
( Its just the first one I saw )
Personally, I'd say its more trouble than its worth, I tried this myself, and found how difficult it can be.
I could probably do it now that I know a bit more, but there are mutliple versions of this by several poeple already,
so I'd just use one of them, or modify one to fit your needs.
I believe you need to convert each file to base64 or something. Can't really give you too many details.
Here is a working one:
http://www.computerc...le-permissions/
( Its just the first one I saw )
Edited on 20 July 2015 - 05:57 PM
Posted 20 July 2015 - 08:01 PM
I've got it working for the most part, it's just not writing to the file correctly
Posted 20 July 2015 - 08:25 PM
You don't need to serialize the contents of the file, only the final table. What I like to do when writing these kind of packing utilities is to first write a working version of an actual packed file. For example, lets say I'm writing a self-extracting archive which is capable of extracting files out of it. I write the result program I desire (the self-extracting file, not the packing utility):
Now that I have the desired result in front of my eyes I can start creating the packing utility, based on the above code:
As you can see it's not that hard, as some might think. You simply have to plan what you're going to do first, before starting to write the code. In this case I suggest to first write the actual extractor program and then write the packing utility.
local files = { --# define all the files I need to extract in a table
["program.lua"] = "print('This is a program')";
["file.txt"] = "This is a text file.";
}
local dir = shell.dir()
for name, content in pairs(files) do --# for every file
local fh = fs.open(fs.combine(dir, name), "w") --# open the file and write the contents
fh.write(content)
fh.close() --# don't forget to close the handle!
end
Now that I have the desired result in front of my eyes I can start creating the packing utility, based on the above code:
local args = {...}
local files = {} --# this table will hold all the files
--# note that the 'files' table structure will be the same as the one in the above code
for i, path in ipairs(args) do --# for every passed file
local fh = fs.open(shell.resolve(path), "r") --# resolve the file and read it
files[fs.getName(path)] = fh.readAll() --# save the contents using the file's name as the key
fh.close()
end
local fh = fs.open("packed_files", "w") --# open the output file
fh.write("local files = ") -- # start the self-extractor code (based on the above code)
fh.writeLine(textutils.serialize(files)) --# serialize the files
--# multiline strings makes it so much easier! (I just pasted the rest of the code there)
fh.writeLine[[
local dir = shell.dir()
for name, content in pairs(files) do --# for every file
local fh = fs.open(fs.combine(dir, name), "w") --# open the file and write the contents
fh.write(content)
fh.close() --# don't forget to close the handle!
end
]]
fh.close() --# don't forget to save the file by closing the handle!
As you can see it's not that hard, as some might think. You simply have to plan what you're going to do first, before starting to write the code. In this case I suggest to first write the actual extractor program and then write the packing utility.
Edited on 20 July 2015 - 06:30 PM
Posted 20 July 2015 - 08:38 PM
My problem is that when I serialize anything it makes it so that when I write it to file it writes
local data = {"code here\
new line in code"}
Instead of
local data={"code here\nnew line in code"}
Posted 20 July 2015 - 09:08 PM
Hehe, that's OK! The program will compile just fine. This method of writing new lines in single-line strings is not very common but is usable in most, if not all, of the programming languages. For example, this line of code is perfectly fine:
print("First line\
second line\
third line.")
Posted 20 July 2015 - 09:10 PM
I just tried it and it worked. I'm a noob
Posted 20 July 2015 - 09:15 PM
My problem is that when I serialize anything it makes it so that when I write it to file it writesInstead oflocal data = {"code here\ new line in code"}
local data={"code here\nnew line in code"}
You can use string.gsub(…), which substitutes every occurrence of a so-called pattern in the original string by something else. The pattern I would use in this case is something like "\n" and the replacement string something like "\\n". More about string.gsub here: http://www.lua.org/m...pdf-string.gsub
I just tried it and it worked. I'm a noob
Edit: got ninja'd. Glad it works now.
Edited on 20 July 2015 - 07:16 PM