Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Gladox114

How to use Filesystem API

Question

I want to make an application that creates a file named "config.txt" and read/write to it but i am hanging on creating the file. Can't find how to create the file.

 

 

My Project: Github_Project_Line85

I want to save the IP that the user writes in and after rebooting it should stay. (Line 85)

I want to make a check system with the config.txt and then load the variable into it.

It's using the GUI API from MineOS

 

Share this post


Link to post
Share on other sites

6 answers to this question

Recommended Posts

  • 0
Quote

file:write(newIP)

That should write to the first line or to a new line?

 

IP = file:read(1)?

 

What is with multiple settings? How should i use the write function then?

Can you give me an example how someone uses such system?

 

 

Share this post


Link to post
Share on other sites
  • 0

It writes to the current line of a new file, so in this case it'd be the 1st line. To read the first line just open the file in read mode `

file = io.open("myfile", "r")

Ip = file:read("*l")

file:close()

`

*l reads 1 line from a file 1 reads one byte from a file

Share this post


Link to post
Share on other sites
  • 0

But what if I want to read the third line? do I need to:

--

file:read("*l")

file:read("*l")

x = file:read("*l")

--

or can i type the number somewhere somehow?

 

edit: Marked a section where I replaced all "write" with "read" because I'm stupid

Share this post


Link to post
Share on other sites
  • 0

Firstly you're mixing up file:read and file:write.. One writes to the file and the other reads.

local config = {}
for line in io.lines "/path/to/config.txt" do
  table.insert(config, line)
end

print(config[3]) -- # 3rd line in your config
-- # this is probably the simplest way but there are others.

To read a specific line in a file you'd have to know the exact offset of bytes until the information you're trying to read.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.