Text after newline is duplicated when saved
Posted: Tue Jun 19, 2007 3:35 am
Hi.
I'm just learning about PureBasic and hit a problem I've not been able to resolve. The following code (cut to bare essentials) will save what's in the main window but repeats information that follows a newline. #HOME needs to be changed to the working directory but then if some text includes some newlines in the middle, the line after will be repeated. In fact you can keep pressing save then open and more text keeps getting added.
Could anyone please tell me what I'm doing wrong because I just can't see it Thank you.
I'm just learning about PureBasic and hit a problem I've not been able to resolve. The following code (cut to bare essentials) will save what's in the main window but repeats information that follows a newline. #HOME needs to be changed to the working directory but then if some text includes some newlines in the middle, the line after will be repeated. In fact you can keep pressing save then open and more text keeps getting added.
Could anyone please tell me what I'm doing wrong because I just can't see it Thank you.
Code: Select all
Enumeration
#WINDOW
#NOTES
#SAVE
#OPEN
EndEnumeration
Declare Save()
Declare Load()
#HOME = "/home/user/dir/"
If OpenWindow(#WINDOW, 0, 0, 750, 500, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
If CreateGadgetList(WindowID(#WINDOW))
EditorGadget(#NOTES,130,5,615,490)
ButtonGadget(#SAVE,5,465,120,30,"Save")
ButtonGadget(#OPEN,5,420,120,30,"Open")
Repeat
Global Event.l = WaitWindowEvent()
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case #SAVE
Save()
Case #OPEN
Load()
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
EndIf
End
Procedure Save()
Define Loop.l
Define TmpString.s
Define TotalItems.l
TotalItems = CountGadgetItems(#NOTES)
For Loop=0 To TotalItems
TmpString + GetGadgetItemText(#NOTES,Loop) + Chr(10)
Next
If CreateFile(0, #HOME + "file.txt")
WriteString(0, TmpString)
CloseFile(0)
Else
MessageRequester("Information","Can't create the file!")
EndIf
EndProcedure
Procedure Load()
ClearGadgetItemList(#NOTES)
If ReadFile(0, #HOME + "file.txt")
While Eof (0) = #False
AddGadgetItem(#NOTES,-1,ReadString(0))
Wend
CloseFile(0)
EndIf
EndProcedure