Jump to content




how to print document?


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

#1 YUGATUG

  • Members
  • 56 posts

Posted 05 April 2015 - 03:31 PM

How do you print the contents of a text file?

#2 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 05 April 2015 - 03:34 PM

View PostYUGATUG, on 05 April 2015 - 03:31 PM, said:

How do you print the contents of a text file?
local f = fs.open("file", "r")
local contents = f.readAll()
f.close()
print(contents)
I hope it helps.
You might want to take a look at the fs API.

Edited by CrazedProgrammer, 05 April 2015 - 03:42 PM.


#3 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 05 April 2015 - 05:56 PM

if you mean the printer block you may be wanting to look at the printer API

#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 05 April 2015 - 06:19 PM

The edit program gives you the option to print

When editing a file, hit ctrl to open the menu, and one of the options should be print

If you want to make a program that prints a file, you'll need to use the printer API (Link in lupus's post above)

#5 YUGATUG

  • Members
  • 56 posts

Posted 05 April 2015 - 06:44 PM

I meant print as in print(), i am about to test the code of your first post

Edited by YUGATUG, 05 April 2015 - 06:44 PM.


#6 YUGATUG

  • Members
  • 56 posts

Posted 05 April 2015 - 06:56 PM

it returns an error at line 8: "attempt to call nil", help?
pastebin.com/jaYn3F0b

Edited by YUGATUG, 05 April 2015 - 07:16 PM.


#7 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 05 April 2015 - 07:00 PM

The writing to the file is good, reading, not so much.


This should work:
--# Write to file
myDocument = {"yolo"}
myFile = fs.open("myFile.txt", "w")
myFile.writeLine(myDocument[1])
myFile.close()

--# Read file
myFile = fs.open("myFile.txt", "r")
contents = myFile.readAll()
myFile.close()

--# Print file
print(contents)


#8 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 05 April 2015 - 07:00 PM

You closed the file before reading it.

#9 TechedZombie

  • Members
  • 43 posts
  • LocationIA, USA

Posted 05 April 2015 - 07:22 PM

View PostHPWebcamAble, on 05 April 2015 - 07:00 PM, said:

The writing to the file is good, reading, not so much.


This should work:
--# Write to file
myDocument = {"yolo"}
myFile = fs.open("myFile.txt", "w")
myFile.writeLine(myDocument[1])
myFile.close()

--# Read file
myFile = fs.open("myFile.txt", "r")
contents = myFile.readAll()
myFile.close()

--# Print file
print(contents)



That is what he has for the read already...

View PostCrazedProgrammer, on 05 April 2015 - 07:00 PM, said:

You closed the file before reading it.

No he didn't

#10 CrazedProgrammer

  • Members
  • 495 posts
  • LocationWageningen, The Netherlands

Posted 05 April 2015 - 07:26 PM

View PostTechedZombie, on 05 April 2015 - 07:22 PM, said:

View PostHPWebcamAble, on 05 April 2015 - 07:00 PM, said:

The writing to the file is good, reading, not so much.


This should work:
--# Write to file
myDocument = {"yolo"}
myFile = fs.open("myFile.txt", "w")
myFile.writeLine(myDocument[1])
myFile.close()

--# Read file
myFile = fs.open("myFile.txt", "r")
contents = myFile.readAll()
myFile.close()

--# Print file
print(contents)



That is what he has for the read already...

View PostCrazedProgrammer, on 05 April 2015 - 07:00 PM, said:

You closed the file before reading it.

No he didn't
Did you even look at the pastebin? :P

#11 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 05 April 2015 - 07:28 PM

View PostTechedZombie, on 05 April 2015 - 07:22 PM, said:

That is what he has for the read already...

This was his original:
myFile, myDocument = fs.open("myDocument", "r"), {}--allows document to be written, i don't know what ", {}" does but someone on the forums said to include it
table.insert(myDocument)--not exactly sure what this does
myDocument.close()--done reading

myFile = fs.open("myDocument", "r")
contents = myDocument.readAll()
print(contents)

No, it isn't the same, notice this line (Second to last):
contents = myDocument.readAll() --# myDocument is the table from above, not the file handle

Edited by HPWebcamAble, 05 April 2015 - 07:29 PM.


#12 YUGATUG

  • Members
  • 56 posts

Posted 05 April 2015 - 08:26 PM

Awesome, finally working! I made a thread that went on for two pages and couldn't get it to work, thanks a million man!

Another question: If i read from a document, then write to it before executing any other command, do i have to use fs.close()?

#13 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 05 April 2015 - 08:32 PM

ALWAYS close your files

#14 YUGATUG

  • Members
  • 56 posts

Posted 05 April 2015 - 09:06 PM

Yes master, please don't hurt me!
jk, thanks for tip :P

#15 YUGATUG

  • Members
  • 56 posts

Posted 06 April 2015 - 07:33 PM

Another question: using writeLine(), myDocument gets written to the first line of myDocument.txt, but i want myDocument to be added to myDocument.txt. how would i do this?

#16 Desolstice

  • Members
  • 7 posts

Posted 06 April 2015 - 07:38 PM

You might want to look into the append mode for fs.open. This allows for you to add things onto the end of the file instead of the file being erased when it is opened.

[code]
temp = fs.open("example","a")
temp.writeLine("hello")
temp.close()
[code]

This would write to the end of the file.


#17 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 06 April 2015 - 07:52 PM

you want the file name at the top of the file?
http://computercraft...wiki/Fs.getName

#18 YUGATUG

  • Members
  • 56 posts

Posted 06 April 2015 - 08:08 PM

not at all... when i apply this to program, i'm going to have this syntax: "Enderchest add <string>", and i want all strings added using that command to be stored, but using the method i currently am, if i type "Enderchest add yolo" then "Enderchest add gg" yolo will not be stored, but i want it to be.

#19 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 06 April 2015 - 08:11 PM

can you post the code?

#20 YUGATUG

  • Members
  • 56 posts

Posted 06 April 2015 - 08:16 PM

pastebin.com/Hm8YAJtw
Getting an error on line 10, don't know why. help? fixed the error, put 3 dots instead of 2 to combine strings.

Edited by YUGATUG, 06 April 2015 - 08:28 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users