Jump to content




[Snippet] Using fs.open, reading & writing from a file.


19 replies to this topic

#1 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 01 March 2012 - 08:40 AM

I'm too lazy to write stuff; so I'll start with this piece of code:

I'll only be using fs.*; since io.open is basically just a wrapper for fs.open.
Look through the io API in /rom/apis/ if you want to see how it works.
Code:
Spoiler
Some Notes:
1) Remember to close your file handles!

2) Note that with fs.open, you'll have to use filehandle.write, not filehandle:write! (or you'll get Table: 1337B33F in your file!)

3) Remember to close your file handles!

4) Yes, I know there's no space between the 0s and "digits". Might fix it later, though.

5) If you want to write multiple lines of data, you'll have to use newlines to separate them (n), as filehandle.write() doesn't automatically append one!

6) I hope this short code sample helped you, and most importantly of all:

7) Remember to close your file handles!

8) If you missed it, here's the pastebin link: http://pastebin.com/nq63aA39

9) Some more info on fs.open/fs file objects:

You can use fsfile.writeLine(text) to write and add a newline automatically.


Supported modes:
r - Read only
w - write only
a - append
b - binary (can be used in conjuction with above: rb, wb, ab)

Edited by Advert, 12 March 2012 - 04:27 AM.
Added some stuff.


#2 ENET

  • Members
  • 15 posts

Posted 06 March 2012 - 11:40 PM

io is a wrapper for fs? Bahahaha I guess that is why io is a standard lua api and fs is a computercraft api.. But you know its whatevs. Personally I suggest fs anyways just because it takes the directory from start where as io takes the directory you give it and searches the current directory for the additional directory. If you understand what I'm saying.

Also there is a fileHandle.writeLine(String text); function that adds 'n' for you. :unsure:/>

fileHandle = fs.open('./filename');
for i,v in pairs(fileHandle)do
print(i,v);
end
fileHandle.close();

#3 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 06 March 2012 - 11:41 PM

View PostENET, on 06 March 2012 - 11:40 PM, said:

io is a wrapper for fs? Bahahaha I guess that is why io is a standard lua api and fs is a computercraft api.. But you know its whatevs.


Personally I suggest fs anyways just because it takes the directory from start where as io takes the directory you give it and searches the current directory for the additional directory. If you understand what I'm saying.
Yes, you'd see that if you looked in /rom/apis/io.

#4 ENET

  • Members
  • 15 posts

Posted 06 March 2012 - 11:43 PM

Ya well as the computercraft wiki says it is the standard api io. Guess that lied :unsure:/> I guess the wiki just means its the same setup..

Also there is a fileHandle.writeLine(String text); function that adds 'n' for you.

fileHandle = fs.open('./filename');
for i,v in pairs(fileHandle)do
print(i,v);
end
fileHandle.close();

#5 Advert

    Custom Title

  • Moderators
  • 459 posts
  • LocationLondon

Posted 06 March 2012 - 11:49 PM

Not quite.
According to 'help io' ingame:

Quote


io is a standard Lua5.1 API, reimplemented for CraftOS. Not all the features are availiable.
Refer to http://www.lua.org/manual/5.1/ for more information.

I've corrected the wiki.

And yes; I know about writeLine.

I'll edit the OP later.

#6 ENET

  • Members
  • 15 posts

Posted 06 March 2012 - 11:51 PM

Aight cool bruh, just making sure. Personally, I like write better cause i Know that nothing will be added to the file I didn't specify.

#7 Ian-Moone

    The Germ

  • New Members
  • 124 posts
  • LocationLiverpool (no I am not a Scouser Im form Holland)

Posted 28 March 2012 - 08:51 PM

now i know how to fix a small problem with something i wrote.

#8 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 29 October 2012 - 10:30 PM

Writing deletes anything that was already on there.
How do I stop that?

#9 LucasUK

  • Members
  • 64 posts

Posted 30 October 2012 - 04:44 PM

instead of fs.open (file, "w") --Write mode, replace the file with whatever

use fs.open (file, "a") ---Append mode, add to the end of the file whaever

#10 rhyleymaster

  • Members
  • 186 posts
  • LocationCanada

Posted 09 November 2012 - 07:15 PM

Thanks for this! I needed help with this for my OS.

P.S.

Quote

1337B33F

LOLOLOLOL

#11 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 15 January 2013 - 07:22 AM

How do U use newline (\n) since for me the results print a damn Question Mark (?)
And doesn't change line!

result = "I'm sooo tired now so i'll go grab me a sandwich \n And Ofcourse It Prints a question mark!"
print(result)
--This is How it will look

I'm sooo tired now so i'll go grab me a sandwich ? And Ofcourse It Prints a question mark! 


#12 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 15 January 2013 - 10:49 AM

View PostHellkid98, on 15 January 2013 - 07:22 AM, said:

How do U use newline (\n) since for me the results print a damn Question Mark (?)
And doesn't change line!

result = "I'm sooo tired now so i'll go grab me a sandwich \n And Ofcourse It Prints a question mark!"
print(result)
--This is How it will look

I'm sooo tired now so i'll go grab me a sandwich ? And Ofcourse It Prints a question mark!
Why are you asking this here? And it should print it just fine, are you sure you're using print() and not term.write() or something?

#13 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 15 January 2013 - 12:13 PM

View PostOrwell, on 15 January 2013 - 10:49 AM, said:

And it should print it just fine, are you sure you're using print() and not term.write() or something?
Could be trying to print on a monitor...

#14 TheOddByte

    Lazy Coder

  • Members
  • 1,607 posts
  • LocationSweden

Posted 15 January 2013 - 12:50 PM

View PostTheOriginalBIT, on 15 January 2013 - 12:13 PM, said:

View PostOrwell, on 15 January 2013 - 10:49 AM, said:

And it should print it just fine, are you sure you're using print() and not term.write() or something?
Could be trying to print on a monitor...
The reason that I posted this here was because I saw it at the pic at the top adn then I thought about it..
And yeah.. I'm using write or rather a function that is like this,
function drawAt(x,y,text)
term.setCursorPos(x,y)
write(text)
end
So all i gotta do is print instead of writing,
I'm feeling so stupid right now.... <_<

#15 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 15 January 2013 - 01:13 PM

View PostTheOriginalBIT, on 15 January 2013 - 12:13 PM, said:

View PostOrwell, on 15 January 2013 - 10:49 AM, said:

And it should print it just fine, are you sure you're using print() and not term.write() or something?
Could be trying to print on a monitor...
Even if he's doing that by redirecting the term and then calling print() or write(), it should work just fine. If you mean he did mon.write(), then it's the same as I meant. :)

View PostHellkid98, on 15 January 2013 - 12:50 PM, said:

View PostTheOriginalBIT, on 15 January 2013 - 12:13 PM, said:

View PostOrwell, on 15 January 2013 - 10:49 AM, said:

And it should print it just fine, are you sure you're using print() and not term.write() or something?
Could be trying to print on a monitor...
The reason that I posted this here was because I saw it at the pic at the top adn then I thought about it..
And yeah.. I'm using write or rather a function that is like this,
function drawAt(x,y,text)
term.setCursorPos(x,y)
write(text)
end
So all i gotta do is print instead of writing,
I'm feeling so stupid right now.... <_<
write() should break lines on '\n' as well, it's a function from bios.lua . Are you sure that you didn't use the write function of term or a monitor? (like term.write("bla") ) The raw write functions of terminal and monitors doesn't break on newlines.

#16 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 01 September 2014 - 06:38 PM

How do I make it so it reads a file, and prints it in a way that you can edit it?

#17 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 01 September 2014 - 06:54 PM

There are many ways of doing it, but probably what most people would do is: read the file and put all lines of it into a numerically-indexed table, make a loop which would handle events and draw those lines. In that loop you should be listening for appropriate events, such as 'char' to get the typed character, 'key' to get keys such as arrow keys, backspace, enter, etc.. Note that this is not a simple thing to do for a beginner. You can also look at how the 'edit' (rom/programs/edit) handles file editing.

Edited by MKlegoman357, 01 September 2014 - 06:55 PM.


#18 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 01 September 2014 - 08:26 PM

View PostMKlegoman357, on 01 September 2014 - 06:54 PM, said:

There are many ways of doing it, but probably what most people would do is: read the file and put all lines of it into a numerically-indexed table, make a loop which would handle events and draw those lines. In that loop you should be listening for appropriate events, such as 'char' to get the typed character, 'key' to get keys such as arrow keys, backspace, enter, etc.. Note that this is not a simple thing to do for a beginner. You can also look at how the 'edit' (rom/programs/edit) handles file editing.

Im not actually that much of a beginner, im actually making an OS but loops and tables i dont even know

But I looked at it again and I see what you mean! :D Now im gonna look up something that prints a file in lines

But anyways thanks, ill take a look at the rom stuffs

Edited by bluebird173, 01 September 2014 - 08:42 PM.


#19 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 02 September 2014 - 08:01 AM

View Postbluebird173, on 01 September 2014 - 08:26 PM, said:

Im not actually that much of a beginner, im actually making an OS but loops and tables i dont even know
those statements are at opposites. loops are a basic programming construct, you'll have large problems making any program without them! Make sure to read through the Lua 5.1 Reference Manual, pretty much everything in that resource you'll need to have at least a basic understanding in to make an effective OS.

Edited by theoriginalbit, 02 September 2014 - 08:01 AM.


#20 Gumball

  • Members
  • 254 posts
  • LocationFairbanks, Alaska

Posted 13 September 2014 - 05:30 AM

Nvm ive learned alot now, I know even know tArgs (Yay!) and loops I learned, AND DEY SOOO USEFUL!




Edited by bluebird173 0 times





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users