Jump to content




Help me! fs.open


  • You cannot reply to this topic
10 replies to this topic

#1 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 04:30 PM

Hi!
How to use fs.open() and how to edit files with programs?
Please don't give me links to computercft wiki, because I don't understand the entire cc wiki.
Thanks.

#2 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 16 May 2014 - 04:58 PM

local fileHandle = fs.open("/path/to/file", "w") -- Open /path/to/file with write permissions

fileHandle.write("Some Text") -- Replaces the current contents of /path/to/file with "Some Text"

fileHandle.close() -- Stops editing, you should always do this.


#3 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 05:04 PM

Thanks very much. And please say me how can I read things.
Also again thanks very much I've waited 1 month ago for this, all people gave only links to wikipedia.

#4 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 16 May 2014 - 05:07 PM

Sure.

local fileHandle = fs.open("/path/to/file", "r") -- Open /path/to/file with read permissions

local contents = fileHandle.readAll() -- Saves the contents of /path/to/file to the contents variable

fileHandle.close() -- Stops reading

Edited by ZudoHackz, 17 May 2014 - 02:02 PM.


#5 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 05:16 PM

And the contents variable will a table?
If table then the indexes are for lines?

Thanks

#6 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 16 May 2014 - 05:21 PM

No, contents will be a string containing the entire file. If you wanted to get an individual line, you would need to do something like this.

**WARNING - UNTESTED BAD CODE**

local fileHandle = fs.open("/path/to/file", "r") -- Open /path/to/file with read permissions

local lineToGet = 5 -- Which line to read?
local lineContents = "" -- Will contain the contents of that line


for _ = 1, lineToGet do -- Repeats until it reaches the right line.
 lineContents = fileHandle.readLine() -- Reads the line.
end

fileHandle.close()

Edited by ZudoHackz, 16 May 2014 - 05:22 PM.


#7 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 16 May 2014 - 05:22 PM

Thanks very much :-)
It helped a lot.
Thanks very much
:-) :-)

#8 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 16 May 2014 - 05:23 PM

No problem :)

#9 Konlab

  • Members
  • 595 posts
  • LocationKerbin

Posted 17 May 2014 - 05:57 AM

And fs.writeLine() works too?

#10 Zudo

  • Members
  • 800 posts
  • LocationUK

Posted 17 May 2014 - 01:20 PM

View PostKonlab, on 17 May 2014 - 05:57 AM, said:

And fs.writeLine() works too?

Do you want to append text onto the end of the file? writeLine() just appends an EOL character to what you entered. If you want to append text, change:

local fileHandle = fs.open("/path/to/file", "w") -- Open /path/to/file with write permissions

to

local fileHandle = fs.open("/path/to/file", "a") -- Open /path/to/file with append permissions

Edited by ZudoHackz, 17 May 2014 - 01:23 PM.


#11 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 17 May 2014 - 01:33 PM

View PostZudoHackz, on 16 May 2014 - 05:21 PM, said:

No, contents will be a string containing the entire file.
No. Given this line

View PostZudoHackz, on 16 May 2014 - 05:07 PM, said:

local contents = fileHandle.read()
only the first line of the file would be in contents, you need to use readAll to have all the contents.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users