Reading everyline from a TXT?

Just starting out? Need help? Post your questions and find answers here.
sharkbate24
User
User
Posts: 50
Joined: Sat Aug 30, 2008 3:21 pm

Reading everyline from a TXT?

Post 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.
[Registed PB User]
[Windows XP SP2 | PureBasic 4.20]
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

Add #CRLF$ at the end of each line
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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...
oh... and have a nice day.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
sharkbate24
User
User
Posts: 50
Joined: Sat Aug 30, 2008 3:21 pm

Post 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!
[Registed PB User]
[Windows XP SP2 | PureBasic 4.20]
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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 "="
oh... and have a nice day.
sharkbate24
User
User
Posts: 50
Joined: Sat Aug 30, 2008 3:21 pm

Post 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.
[Registed PB User]
[Windows XP SP2 | PureBasic 4.20]
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

so there are additional chars in your textfile that can't be displayed properly.

... what kind of textfile is it?
oh... and have a nice day.
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

What kind of gadget are you writing the text to?
I may look like a mule, but I'm not a complete ass.
sharkbate24
User
User
Posts: 50
Joined: Sat Aug 30, 2008 3:21 pm

Post 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.
[Registed PB User]
[Windows XP SP2 | PureBasic 4.20]
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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
I may look like a mule, but I'm not a complete ass.
sharkbate24
User
User
Posts: 50
Joined: Sat Aug 30, 2008 3:21 pm

Post 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.
[Registed PB User]
[Windows XP SP2 | PureBasic 4.20]
Post Reply