Page 1 of 2

Reading everyline from a TXT?

Posted: Thu Oct 30, 2008 7:44 pm
by sharkbate24
Hey everyone,

So I have a TXT file with 3 lines in it. I use SetGadgetText to put the txt in, but for some reason, only the last line is being added.

Any idea's what script I should use?

I'm using While Eof = 0.

Thanks.

Posted: Thu Oct 30, 2008 7:48 pm
by ts-soft
Add #CRLF$ at the end of each line

Posted: Thu Oct 30, 2008 7:49 pm
by Kaeru Gaman
let me guess... you use a loop, and you have ReadString and SetGadgetText in every turn of the loop?

... if yes, read the sentence carefully more than once...

Posted: Thu Oct 30, 2008 7:52 pm
by srod
Now now Kaeru, what kind of help is that? tut tut!

@sharkbate24 : read the ****ing manual!

:twisted:

Only kidding; do what ts-soft suggests and append each read string to a string variable with #CRLF$ added to each string and then, when you've read all the data, use SetGadgetText() etc.

Posted: Thu Oct 30, 2008 7:59 pm
by sharkbate24
srod wrote:Now now Kaeru, what kind of help is that? tut tut!

@sharkbate24 : read the ****ing manual!

:twisted:

Only kidding; do what ts-soft suggests and append each read string to a string variable with #CRLF$ added to each string and then, when you've read all the data, use SetGadgetText() etc.
Thanks guys. Sorry about that hehe, I kept browsing the manual and there was nothing about a new line. My apologies.

EDIT: I got another problem. The code I have so far is:


Code: Select all

        While Eof(#UpdateInfo) = 0
            updateinfo$ = ReadString(#UpdateInfo) + #CRLF$
        Wend
        
        SetGadgetText(#String_1, updateinfo$)
But when I look at the #String_1 in the preview, it has squares where the spaces are meant to be. Any idea's?

Thanks!

Posted: Thu Oct 30, 2008 8:08 pm
by ts-soft

Code: Select all

If ReadFile(0, "text.txt")
  While Not Eof(0)
    AddGadgetItem(#edit, -1, ReadString(0))
  Wend
  CloseFile(0)
EndIf

; next way:
If ReadFile(0, "text.txt")
  While Not Eof(0)
    Text.s + ReadString(0) + #CRLF$
  Wend
  SetGadgetText(#edit, Text)
  CloseFile(0)
EndIf

; next way:
If ReadFile(0, "text.txt")
  length = Lof(0)
  Text.s = Space(length)
  ReadData(0, @Text, length)
  SetGadgetText(#edit, Text)
  CloseFile(0)
EndIf
this should all work for you :wink:

Posted: Thu Oct 30, 2008 8:09 pm
by srod
Alternatively, if for example filling an editor gadget, then you can use AddGadgetItem() to add each line separately (no #CRLF$ required).

**EDIT : sharkbate, the debugger will substitute EOL's for squares etc. :)

****EDIT : @ts-soft, that 3rd method is not really advisable in case the text file holds text in utf-8 format.

Posted: Thu Oct 30, 2008 8:13 pm
by Kaeru Gaman
The code I have so far is:

Code: Select all

        While Eof(#UpdateInfo) = 0
            updateinfo$ = ReadString(#UpdateInfo) + #CRLF$
        Wend
       
        SetGadgetText(#String_1, updateinfo$)
in every loopturn you completely overwrite the previous line.
if you want to add up lines, you have to use a "+", not "="

Posted: Thu Oct 30, 2008 8:15 pm
by sharkbate24
Kaeru Gaman wrote:
The code I have so far is:

Code: Select all

        While Eof(#UpdateInfo) = 0
            updateinfo$ = ReadString(#UpdateInfo) + #CRLF$
        Wend
       
        SetGadgetText(#String_1, updateinfo$)
in every loopturn you completely overwrite the previous line.
if you want to add up lines, you have to use a "+", not "="
Thanks guys. Yeah, I realised that after looking at what each line did.

Also I've tried what everyone has told me here, but I'm still getting squares in between each line.

Any idea's?

Thanks everyone.

Posted: Thu Oct 30, 2008 8:18 pm
by Kaeru Gaman
so there are additional chars in your textfile that can't be displayed properly.

... what kind of textfile is it?

Posted: Thu Oct 30, 2008 8:19 pm
by ts-soft
I forgot the last methode (windows only)

Code: Select all

Procedure StreamFileIn_Callback(hFile, pbBuff, cb, pcb)
  ProcedureReturn ReadFile_(hFile, pbBuff, cb, pcb, 0)!1
EndProcedure

Procedure Editor_LoadText(Gadget, File.s)
  Protected FF
  Protected StreamData.EDITSTREAM
  FF = ReadFile(#PB_Any, File)
  If FF
    StreamData\dwCookie = FileID(FF)
    StreamData\dwError = #Null
    StreamData\pfnCallback = @StreamFileIn_Callback()
    SendMessage_(GadgetID(Gadget), #EM_STREAMIN, #SF_TEXT, @StreamData)
    CloseFile(FF)
  EndIf
EndProcedure

Posted: Thu Oct 30, 2008 8:20 pm
by srod
What kind of gadget are you writing the text to?

Posted: Thu Oct 30, 2008 8:23 pm
by sharkbate24
Kaeru Gaman wrote:so there are additional chars in your textfile that can't be displayed properly.

... what kind of textfile is it?
Basically, where the new lines are, it's getting replaced by a square. It's a TXT file. The gadget is a text box.

Thanks.

Posted: Thu Oct 30, 2008 8:47 pm
by srod
Try the following. Any squares?

Code: Select all

If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  TextGadget(0, 10,  10, 250, 60, "TextGadget Standard (Left)")
  a$ = "line 1" + #CRLF$ + "line 2"
  SetGadgetText(0, a$)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf

Posted: Thu Oct 30, 2008 8:51 pm
by sharkbate24
srod wrote:Try the following. Any squares?

Code: Select all

If OpenWindow(0, 0, 0, 270, 160, "TextGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  TextGadget(0, 10,  10, 250, 60, "TextGadget Standard (Left)")
  a$ = "line 1" + #CRLF$ + "line 2"
  SetGadgetText(0, a$)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Nope, no squares.

Thanks.