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

FS API Help Please

Started by AppleManYT, 22 July 2015 - 10:44 AM
AppleManYT #1
Posted 22 July 2015 - 12:44 PM
Hello everybody!
i want to make a simple program that can read a line of text from a file, for example if i had a program called "read" and had a file called "text" when i run "read" it prints out on the screen all the text in the "text" file. This is really basic but i just can't figure it out! Please Help! :)/> :)/> :)/>

Thanks, Apple
Balthamel #2
Posted 22 July 2015 - 07:47 PM
Post what you have so far, people are a lot more likely to help you fix what you have than just hand you free code.
flaghacker #3
Posted 22 July 2015 - 08:25 PM
Take a look at this page. It should get you started. If there's anything you get stuck with, feel free to ask in this topic.
Zenon #4
Posted 22 July 2015 - 08:53 PM
Hello everybody!
i want to make a simple program that can read a line of text from a file, for example if i had a program called "read" and had a file called "text" when i run "read" it prints out on the screen all the text in the "text" file. This is really basic but i just can't figure it out! Please Help! :)/> :)/> :)/>

Thanks, Apple

Alright, well, first you need to have JUST TEXT in the "text" file, then you could do something like this:

file = fs.open("text")
fileText = file.readAll()
monitor.print(fileText)
Though doing that could make the text not fit, you could also read individual lines with "fs.readLine()" then print that on a new line each time. Hope that helped! ^.^
Dragon53535 #5
Posted 22 July 2015 - 09:17 PM
–snip–
Alright, well, first you need to have JUST TEXT in the "text" file,
–snip–
Anything in the 'text' file will ALWAYS be text unless you specify so after reading the file.

Example text file:

hi
cake
whatchuwant2do
Here are some numbers 25723598275982759
Example read file:

local file = fs.open('text','r')
local line = file.readLine()
repeat
  print(line)
  line = file.readLine()
until not line
file.close()
Boom.

A little Fs API tutorial for you
MKlegoman357 #6
Posted 22 July 2015 - 09:18 PM
Alright, well, first you need to have JUST TEXT in the "text" file, then you could do something like this:

file = fs.open("text")
fileText = file.readAll()
monitor.print(fileText)
Though doing that could make the text not fit, you could also read individual lines with "fs.readLine()" then print that on a new line each time. Hope that helped! ^.^

The example here is broken and wrong. First, there is no global 'monitor' variable. Even if you meant for it to be a wrapped monitor peripheral, monitors don't have a method 'print'. Second, you don't close the file handle.

Here's how you'd use the fs API to open, read and close a file:


local file = fs.open( "file.txt", "r" ) --# open the file 'file.txt' in read mode

local contents = file.readAll() --# get all of the text inside the file

file.close() --# close the file handle. THIS IS VERY IMPORTANT. ALWAYS CLOSE THE FILE HANDLE.

--# after reading the file the 'contents' variable will hold the contents of the file

print( contents ) --# print the contents of the file
Edited on 22 July 2015 - 07:18 PM
Zenon #7
Posted 22 July 2015 - 09:28 PM
Spoiler
–snip–
Alright, well, first you need to have JUST TEXT in the "text" file,
–snip–
Anything in the 'text' file will ALWAYS be text unless you specify so after reading the file.

Example text file:

hi
cake
whatchuwant2do
Here are some numbers 25723598275982759
Example read file:

local file = fs.open('text','r')
local line = file.readLine()
repeat
  print(line)
  line = file.readLine()
until not line
file.close()
Boom.

A little Fs API tutorial for you
I meant not having something like this

print("Hi")
Becuase that would show up weird :P/>

Spoiler
Alright, well, first you need to have JUST TEXT in the "text" file, then you could do something like this:

file = fs.open("text")
fileText = file.readAll()
monitor.print(fileText)
Though doing that could make the text not fit, you could also read individual lines with "fs.readLine()" then print that on a new line each time. Hope that helped! ^.^

The example here is broken and wrong. First, there is no global 'monitor' variable. Even if you meant for it to be a wrapped monitor peripheral, monitors don't have a method 'print'. Second, you don't close the file handle.

Here's how you'd use the fs API to open, read and close a file:


local file = fs.open( "file.txt", "r" ) --# open the file 'file.txt' in read mode

local contents = file.readAll() --# get all of the text inside the file

file.close() --# close the file handle. THIS IS VERY IMPORTANT. ALWAYS CLOSE THE FILE HANDLE.

--# after reading the file the 'contents' variable will hold the contents of the file

print( contents ) --# print the contents of the file
I did mean a wrapped monitor, thats why I didnt use "m" so they would know I meant a monitor, though I shouldve explained better. Also, I assumed that monitors had a print function aswell as write, like the default terminal, but I havent used monitors much lol . . . Thanks for the heads up. :D/>
AppleManYT #8
Posted 22 July 2015 - 10:47 PM
The example here is broken and wrong. First, there is no global 'monitor' variable. Even if you meant for it to be a wrapped monitor peripheral, monitors don't have a method 'print'. Second, you don't close the file handle.

Here's how you'd use the fs API to open, read and close a file:


local file = fs.open( "file.txt", "r" ) --# open the file 'file.txt' in read mode

local contents = file.readAll() --# get all of the text inside the file

file.close() --# close the file handle. THIS IS VERY IMPORTANT. ALWAYS CLOSE THE FILE HANDLE.

--# after reading the file the 'contents' variable will hold the contents of the file

print( contents ) --# print the contents of the file

Thanks For The Help Guys! - I Will Try That Code Now.