![]() |
|
reading and writing to a file - Printable Version +- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk) +-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html) +--- Forum: Starfall (https://teamscm.co.uk/forum-7.html) +--- Thread: reading and writing to a file (/thread-1028.html) |
reading and writing to a file - shinji144 - 27-06-2015 I'm trying to save my ship's 'crew' to a text file, but what I'm doing doesn't seem to be making the text file and writing to it (or I just can't find it..). Ideally I'll be serializing my 'crew-members' table using vON, and then writing the resulting string into the text file. The crew file will be loaded every time my ship gets spawned. For now, here is my test code that I've been trying to run: Code: local _savedCrewPath = "ShipCrewFile.txt"
local _savedCrewFile = nil
_savedCrewFile = io.open(_savedCrewPath, "w")
_savedCrewFile:write("asdf123")
_savedCrewFile:close()
_savedCrewFile = io.open(_savedCrewPath, "r")
io.input(_savedCrewFile)
print(io.read())Once I get this figured out I'll be happy to make a tutorial on this topic. RE: reading and writing to a file - LocInt - 27-06-2015 (27-06-2015, 01:12 AM)shinji144 Wrote: I'm trying to save my ship's 'crew' to a text file, but what I'm doing doesn't seem to be making the text file and writing to it (or I just can't find it..). Ideally I'll be serializing my 'crew-members' table using vON, and then writing the resulting string into the text file. The crew file will be loaded every time my ship gets spawned. Code: -- Opens a file in read mode if the file exists
File = io.open("permission.txt", "r")
if(File==nil) then
-- If we cant read the file we write a new file
File = io.open("permission.txt", "w")
File:write("")
File:close()
-- and then we open it in append mode so we can append into it
File = io.open("permission.txt", "a")
end
if(File~=nil) then
File:write("yourshithere")
File:close()
endRE: reading and writing to a file - Person8880 - 27-06-2015 The reason your print is yielding nothing is because you're not telling it what to read. If you want to read the entire file, then you use:
The valid input parameters to read are:
You can combine them in any way, for example:
This would read the first two lines (without including the new line characters), then the rest of the file.LocInt: You don't need that complicated setup. Just open the file in append mode from the start if you want to append. It will create the file if it does not exist. RE: reading and writing to a file - LocInt - 27-06-2015 (27-06-2015, 11:31 AM)Person8880 Wrote: The reason your print is yielding nothing is because you're not telling it what to read. If you want to read the entire file, then you use: But im complicated RE: reading and writing to a file - Person8880 - 27-06-2015 Yes, we know.
RE: reading and writing to a file - Red The Blue Color - 28-06-2015 (27-06-2015, 10:10 AM)LocInt Wrote:(27-06-2015, 01:12 AM)shinji144 Wrote: I'm trying to save my ship's 'crew' to a text file, but what I'm doing doesn't seem to be making the text file and writing to it (or I just can't find it..). Ideally I'll be serializing my 'crew-members' table using vON, and then writing the resulting string into the text file. The crew file will be loaded every time my ship gets spawned. Locs way even includes a helpful handy log for your removed crew! ![]() (Unless he changed it) |