Page 1 of 1
how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 2:03 pm
by Lonely_Star77
hello.
i'm trying to figure is there a way to read a whole text file and just print it "drawtext()" it on the screen so it will be displayed exactly as it is in the text file...
here is a text file for a test:
Code: Select all
hello this is a test....
how are you doing?
hello this is a test
this is a teast To Read file text
And print it on screen
end :)
and here is the code i use to test it which is not displaying it correctly - i mean it doesn't break the lines you get a long line of string that goes out of the screen
Code: Select all
#Window_Main = 0
#Screen_Width = 800
#Screen_Height = 600
InitKeyboard()
InitSprite()
OpenWindow(#Window_Main, 0, 0, #Screen_Width, #Screen_Height, "Snake", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window_Main), 0, 0, #Screen_Width, #Screen_Height, #True, 0, 0)
Procedure.s FileToVar(File$)
ReadFile(0, File$)
While Not Eof(0)
VarString$ + ReadString(0) + #CRLF$ ; #CRLF$ is a line break
Wend
ProcedureReturn VarString$
EndProcedure
buffer.s = FileToVar("test2.txt")
Repeat
; Repeat
; Define Event = WindowEvent()
; Select Event
; Case #PB_Event_CloseWindow
; End
; EndSelect
; Until Event = 0
If StartDrawing(ScreenOutput())
DrawText(0,0,buffer)
StopDrawing()
EndIf
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
the procedure FileToVar.s i found after searching the forum...
Re: how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 2:31 pm
by NicTheQuick
I can not help you with DrawText but I can say that the Procedure FileToVar is suboptimal because it does not close the file properly and it is slow to concatenate strings this way. Better use this procedure:
Code: Select all
Procedure.s FileToVar(File.s)
Protected hFile.i
Protected result.s
hFile = ReadFile(#PB_Any, File)
If hFile
result = ReadString(hFile, #PB_File_IgnoreEOL)
CloseFile(hFile)
EndIf
ProcedureReturn result
EndProcedure
Re: how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 2:47 pm
by Axolotl
Hi
i guess that DrawText is only drawing single lines ...
You have to do something like this. (not optimized for speed, performance, what so ever)
I copied the example text in a variable, for an brief example on DrawText i modified the example from Help.
Code: Select all
EnableExplicit
Define filetext$ = "" +
"hello this is a test...." + #LF$ +
" how are you doing?" + #LF$ +
"" + #LF$ +
"" + #LF$ +
"hello this is a test" + #LF$ +
"" + #LF$ +
"this is a teast To Read file text" + #LF$ +
" " + #LF$ +
" And print it on screen" + #LF$ +
" " + #LF$ +
" End :)" + #LF$ +
""
Define t$, index, x, y , Event
Define WW = 400, WH = 400
If OpenWindow(0, 0, 0, WW, WH, "2DDrawing Example", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
If CreateImage(0, WW, WH) And StartDrawing(ImageOutput(0))
DrawingMode(#PB_2DDrawing_Transparent)
Box(0, 0, WW, WH, #White)
For index = 1 To CountString(filetext$, #LF$)
t$ = StringField(filetext$, index, #LF$)
DrawText(x, y, t$, #Black)
y + TextHeight(t$)
Next index
StopDrawing()
ImageGadget(0, 0, 0, WW, WH, ImageID(0))
EndIf
Repeat
Event = WaitWindowEvent()
Until Event = #PB_Event_CloseWindow
EndIf
Re: how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 2:53 pm
by TI-994A
Lonely_Star77 wrote:...read a whole text file and just print it "drawtext()" it on the screen so it will be displayed exactly as it is in the text file......
One way:
Code: Select all
#Window_Main = 0
#Screen_Width = 800
#Screen_Height = 600
Procedure.s FileToVar(File$)
ReadFile(0, File$)
While Not Eof(0)
VarString$ + ReadString(0) + #CRLF$
Wend
CloseFile(0)
ProcedureReturn VarString$
EndProcedure
InitSprite()
InitKeyboard()
OpenWindow(#Window_Main, 0, 0, #Screen_Width, #Screen_Height, "Snake", #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#Window_Main), 0, 0, #Screen_Width, #Screen_Height, #True, 0, 0)
buffer.s = FileToVar("textFile.txt")
Repeat
If Not drawn And StartDrawing(ScreenOutput())
For i = 1 To CountString(buffer, #CRLF$)
DrawText(0, y, StringField(buffer, i, #CRLF$))
y + 30
Next i
StopDrawing()
drawn = #True
EndIf
FlipBuffers()
ExamineKeyboard()
Until KeyboardPushed(#PB_Key_Escape)
Re: how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 3:10 pm
by Axolotl
Small hint on reading text files:
Nowadays it is a good style to check the format of the file in advance with ReadStringFormat(). Especially if the text file was not created by yourself or if different editors are used to create it.
Code: Select all
Procedure.s FileToVar(File.s)
Protected hFile.i, fmt.i
Protected result.s
hFile = ReadFile(#PB_Any, File)
If hFile
fmt = ReadStringFormat(hFile)
If fmt = #PB_Ascii Or ; : No BOM detected. This usually means a plain text file.
fmt = #PB_UTF8 Or ; : UTF-8 BOM detected.
fmt = #PB_Unicode ; : UTF-16 (little endian) BOM detected.
result = ReadString(hFile, #PB_File_IgnoreEOL | fmt)
Else
;' print some error message on this formats
; #PB_UTF16BE: UTF-16 (big endian) BOM detected.
; #PB_UTF32 : UTF-32 (little endian) BOM detected.
; #PB_UTF32BE: UTF-32 (big endian) BOM detected.
EndIf
CloseFile(hFile)
EndIf
ProcedureReturn result
EndProcedure
Happy Coding and stay healthy.
Re: how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 3:18 pm
by Lonely_Star77
thank you all very much!

yes - stay safe and protected
Re: how to read and draw on screen a whole text file?
Posted: Fri Feb 26, 2021 4:08 pm
by NicTheQuick
It is also not efficient to call `CountString(buffer, #CRLF$)` on every iteration. Better store it in a variable and do the loop afterwards.
Code: Select all
Define lines = CountString(buffer, #CRLF$)
;...some code
For i = 1 To lines
DrawText(0, y, StringField(buffer, i, #CRLF$))
y + 30
Next i
Or store the lines in an Array or a LinkedList because `StringField()` is not that efficient too if it is called that much.
In the end it depends on how often you call that loop.