Appending to file

jschuchert

Active Member
Licensed User
Longtime User
I am a brand new user and just getting started but have had experience with other development software like VB and NSB. I am re-doing my application now with basic4ppc. Here is the problem:
When my app starts I want the list box on the first form to populate with the files the user has chosen previously so he can select from that list. He can also enter a new file in a textbox, press the OK button, and it should be appended to that list box. Here is what I have so far:

Sub App_Start
If Not (FileExist ("filelist.txt")) Then
FileOpen (f,"filelist.txt",cAppend,,cASCII) 'this seems to work
FileClose(f)
End If
frmstart.show 'this is the form with the listbox, textbox and OK button
FileOpen(f,"filelist.txt",cRead)
s=FileRead(f)
Do Until s=EOF
lstStart.Add(s)
s=FileRead(f)
Loop
FileClose (f)
End Sub
'the above will only add the last name entered in the listbox (see below)


'here is what happens when the OK button is pressed on the start form
Sub cmdStartOK_Click
strfilename=txtstartfilename.Text 'the textbox where user enters a new name
FileOpen(f2,"filelist.txt",cWrite) 'cAppend throws a syntax error
FileWrite(f2,txtstartfilename.Text )
lststart.Add(strfilename)
FileClose(f2)
End Sub

I know each time OK is pressed that the file starts over because it was opened with 'cWrite' and why only the last name is shown when the app starts again. How do I append the files to the list? It can't be that hard but I am not seeing it right now.

How is a screen shot shown? I could send a file to my web site and then include the url but there must be an easier way. Thanks for any help.

Jim Schuchert
 

Ariel_Z

Active Member
Licensed User
You can zip your sources and upload them.
Anyway - if I get you right you should try:
B4X:
FileOpen (f,"filelist.txt",cWrite,,cASCII)
At line #3, and
B4X:
FileOpen(f2,"filelist.txt",cWrite, cAppend)

Consider this sample code:
B4X:
Sub Globals
   'Declare the global variables here.

End Sub

Sub App_Start
   If Not(FileExist("example.dat")) Then
      FileOpen(f, "example.dat", cWrite) 
      FileWrite(f, "First Line")
   Else
      FileOpen(f, "example.dat", cWrite, cAppend)
      FileWrite(f, "xxx")
   End If
End Sub

Copy, run a few times, open example.dat with notepad and you'll get it.
 

jschuchert

Active Member
Licensed User
Longtime User
You nailed it, my friend. Thank you very much. Nothing in the docs indicate using cWrite and cAppend together.
 

Ariel_Z

Active Member
Licensed User
Actually, if you looked carefully at the help file:

yntax: FileOpen (Connection Name, File Name, cRead | cWrite | cRandom [,cAppend [,cASCII])

Note that the "[" before the word ",cAppend" indicates "optional added". But I must agree there is no way to know it unless you are really used to reading technical stuff and there is no other example or reference.
 

jschuchert

Active Member
Licensed User
Longtime User
You are absolutely correct. In fact, I was going to post that I did read it but it didn't sink in until after seeing your solution. Thanks again.
 
Top