Text after newline is duplicated when saved

Linux specific forum
Dave651
User
User
Posts: 10
Joined: Thu Jun 14, 2007 11:27 pm

Text after newline is duplicated when saved

Post by Dave651 »

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.

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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Text after newline is duplicated when saved

Post by freak »

Code: Select all

  For Loop=0 To TotalItems 
Gadget items are numbered from 0 to CountGadgetItems(gadget)-1.
You loop runs one time too often.

Also you have to cut one more Chr(10) off at the very end, because if
there is a newline at the very end of the file, PB will consider this another
empty line at the end of the file.
quidquid Latine dictum sit altum videtur
Dave651
User
User
Posts: 10
Joined: Thu Jun 14, 2007 11:27 pm

Post by Dave651 »

Thank you Freak, but I still get the problem even after making the change. In fact, if there are multiple newlines in the text, the line after every newline is duplicated. Incidentally, for some reason I don't have this problem when the code is run on a Windows machine.

I cut the code to make it short so there isn't an extra chr(10) in the full version, but thanks for pointing it out.
Dave651
User
User
Posts: 10
Joined: Thu Jun 14, 2007 11:27 pm

Post by Dave651 »

Just to make sure, I tried the same code on Windows machine and there was no problem at all. Does anyone have an idea why there is only a problem on Linux?

Thanks
Post Reply