This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Advert's profile picture

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

Started by Advert, 01 March 2012 - 07:40 AM
Advert #1
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:
SpoilerPastebin: http://pastebin.com/nq63aA39

*Note that I'm using a screenshot; because frankly, I'm tired of the forums killing the formatting if I edit my posts :unsure:/>/>
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
ENET #2
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();
Advert #3
Posted 06 March 2012 - 11:41 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.
Yes, you'd see that if you looked in /rom/apis/io.
ENET #4
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();
Advert #5
Posted 06 March 2012 - 11:49 PM
Not quite.
According to 'help io' ingame:
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.
ENET #6
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.
Ian-Moone #7
Posted 28 March 2012 - 09:51 PM
now i know how to fix a small problem with something i wrote.
Geforce Fan #8
Posted 29 October 2012 - 10:30 PM
Writing deletes anything that was already on there.
How do I stop that?
LucasUK #9
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
rhyleymaster #10
Posted 09 November 2012 - 07:15 PM
Thanks for this! I needed help with this for my OS.

P.S.
1337B33F

LOLOLOLOL
TheOddByte #11
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! 
Orwell #12
Posted 15 January 2013 - 10:49 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!
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?
theoriginalbit #13
Posted 15 January 2013 - 12:13 PM
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…
TheOddByte #14
Posted 15 January 2013 - 12:50 PM
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…. <_</>
Orwell #15
Posted 15 January 2013 - 01:13 PM
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. :)/>

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.
Gumball #16
Posted 01 September 2014 - 07:38 PM
How do I make it so it reads a file, and prints it in a way that you can edit it?
MKlegoman357 #17
Posted 01 September 2014 - 07: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 on 01 September 2014 - 05:55 PM
Gumball #18
Posted 01 September 2014 - 09:26 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.

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 on 01 September 2014 - 07:42 PM
theoriginalbit #19
Posted 02 September 2014 - 09:01 AM
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 on 02 September 2014 - 07:01 AM
Gumball #20
Posted 13 September 2014 - 06: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