Save the textarea text inside a file.

BlitzMax Forums/BlitzMax Programming/Save the textarea text inside a file.

hub(Posted 2011) [#1]
Hi !
How to write to a file each textarea line ?

For example have this inside the textarea :

This is a sample 1.
This is a sample 2.
This is a sample 3.

Now how to write to my text file this :

3
This is a sample 1.
This is a sample 2.
This is a sample 3.

3 is the number of lines stored inside the text file. Next i need to read this and add each line to the textarea.

Many thanks !

Last edited 2011


hub(Posted 2011) [#2]
oups sorry wrong forum.


matibee(Posted 2011) [#3]
like this?..

Local outFile:TStream = WriteFile( "test.txt" )
Local text$ = te.GetText()
Local content$[] = text$.Split( "~n" )
	
WriteLine( outFile, content$.length )
For Local a$ = EachIn content$
	WriteLine ( outFile, a$ )
Next 
	
CloseFile( outFile )


"te" is the the text area.

I'm not sure if that's good enough across all platforms as the mac only uses a newline character and windows uses newline+carriage return. But that's a start.


matibee(Posted 2011) [#4]
Just saw you want to read it back too.

You don't need to write the number of lines to the start of the file though. This works without.

Local inFile:TStream = ReadFile( "test.txt" )
Local text$
While Not Eof( inFile )
	text$ :+ ReadLine( inFile ) + "~n"
wend
CloseFile( inFile )
te.SetText( text$ )



hub(Posted 2011) [#5]
many thanks matibee !


kfprimm(Posted 2011) [#6]
SaveText GadgetText(textarea), "test.txt"
Local text$=LoadText("test.txt")
Print text


Something like this *should* work cross-platform.


hub(Posted 2011) [#7]
yes of course, but i add these informations at the end of an existing file (after other writeline statements).

Last edited 2011