Team Spartan Cookies & Milk Forums
  • Issue Tracker
  • Donations
  • Search
  • Member List
  • Help

Register

Team Spartan Cookies & Milk Forums Garry's Mod Starfall v
« Previous 1 … 13 14 15 16 17 18 Next »
reading and writing to a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Threaded Mode
reading and writing to a file
27-06-2015, 01:12 AM, (This post was last modified: 27-06-2015, 01:17 AM by shinji144.)
#1
shinji144 Offline
TSCM Member
***
Posts: 5
Threads: 2
Joined: Jun 2015
Question  reading and writing to a file
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.
Website Find
Reply
27-06-2015, 10:10 AM, (This post was last modified: 27-06-2015, 10:14 AM by LocInt.)
#2
LocInt Offline
Long gone
****
Posts: 489
Threads: 91
Joined: Aug 2014
RE: reading and writing to a file
(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.

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.

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() end

тѕсм.Person8880 hovers the nerf bat over Tetryon damage
Website Find
Reply
27-06-2015, 11:31 AM,
#3
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: reading and writing to a file
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:
io.input(_savedCrewFile)
print(io.read("*a"))

The valid input parameters to read are:
  • *a - Read the rest of the file from where you're currently at.
  • *n - Reads a number.
  • *l - Reads a line without the new line character.
  • *L - Reads a line with the new line character.
  • Number - Reads the given number of bytes.

You can combine them in any way, for example:
local FirstLine, SecondLine, RestOfFile = io.read( "*l", "*l", "*a" )
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.
Find
Reply
27-06-2015, 11:32 AM,
#4
LocInt Offline
Long gone
****
Posts: 489
Threads: 91
Joined: Aug 2014
RE: reading and writing to a file
(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:
io.input(_savedCrewFile)
print(io.read("*a"))

The valid input parameters to read are:
  • *a - Read the rest of the file from where you're currently at.
  • *n - Reads a number.
  • *l - Reads a line without the new line character.
  • *L - Reads a line with the new line character.
  • Number - Reads the given number of bytes.

You can combine them in any way, for example:
local FirstLine, SecondLine, RestOfFile = io.read( "*l", "*l", "*a" )
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.

But im complicated

тѕсм.Person8880 hovers the nerf bat over Tetryon damage
Website Find
Reply
27-06-2015, 11:38 AM,
#5
Person8880 Offline
gpj.ecafllorT
*******
Posts: 2,407
Threads: 45
Joined: Sep 2013
RE: reading and writing to a file
Yes, we know. Wink
Find
Reply
28-06-2015, 07:03 AM,
#6
Red The Blue Color Offline
TSCM Super Donating Member
****
Posts: 422
Threads: 34
Joined: Feb 2015
RE: reading and writing to a file
(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.

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.

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() end

Locs way even includes a helpful handy log for your removed crew! Big Grin
(Unless he changed it)

"with it's superior software, i have no doubt it will succeed in finding fuck all" - T-17, 2016

[Super Member] [ENCOM] т-ѕсм.Red The Blue Color™: server do you support Hitler?
[Server] srcds_linux: The probabilty is about 99.99%, so interpret that how you want to.
Find
Reply
« Next Oldest | Next Newest »


  • View a Printable Version
Forum Jump:


Users browsing this thread:
Contact UsTeam Spartan Cookies & MilkReturn to TopLite (Archive) ModeMark all forums readRSS Syndication

Powered By MyBB, © 2002-2026 MyBB Group.