File saving help

BlitzPlus Forums/BlitzPlus Programming/File saving help

Falelorn(Posted 2004) [#1]
I want to save the text in a text area as a file, but im having troubles.

here is the code


window = CreateWindow( "Test",0,0,640,480,0,11 )
CentreGadget(Window)

menu=WindowMenu (window)

file=CreateMenu("&File",0,menu)
CreateMenu "Open",1,file
CreateMenu "Save",1,file

UpdateWindowMenu window

txtbox = CreateTextArea (20,20,500,300,window)

Repeat

ID = WaitEvent(15)
If ID=$803 Then End
If ID=$1001 Then
EID=EventData()
Select EID
Case 1 Gosub open
Case 2 Gosub save
End Select
End If

Forever


.open
filz$=RequestFile$( "Pick a file!","txt, dat, qst" )
txt=ReadFile(filz$)
While Not Eof(txt)
r$=ReadLine(txt)
r$=r$+Chr$(13)
AddTextAreaText(txtbox,r)
Wend
CloseFile txt
Return

.save
txt= TextAreaText(txtbox,r)
While Not Eof(txt)
r$=ReadLine(txt)
r$=r$+Chr$(13)
WriteFile("quest.txt")
Wend
Return



Function CentreGadget(G, Group = -1)
If Group = -1 Then Group = Desktop()
CX = ClientWidth(Group) / 2 : CY = ClientHeight(Group) / 2
SetGadgetShape G, CX - (GadgetWidth(G)/2), CY - (GadgetHeight(G)/2), GadgetWidth(G), GadgetHeight(G)
End Function


Falelorn(Posted 2004) [#2]
also how do you do the code area (black background with green text)?


rdodson41(Posted 2004) [#3]
To the text in a text area to a file, try this. It will overwrite any current data:
file=Writefile("textarea.txt")           ;Make new file
WriteLine(file,TextAreaText$(textarea))  ;Write text into file
CloseFile(file)                          ;Close file


Or to append then text to a file with data inside it:
file=Openfile("textarea.txt")             ;Open file
Seekfile(file,FileSize(file))             ;Gets to the end of file
WriteLine$(file,TextAreaText$(textarea))  ;Write text
Closefile(file)                           ;Close file


Look in the command reference for the commands used to format a textarea with color and stuff. Theres 3 or 4 of them.

Hope it helps!


Falelorn(Posted 2004) [#4]
Thanks ill try that.