Trouble Writing Multiple Lines

Blitz3D Forums/Blitz3D Beginners Area/Trouble Writing Multiple Lines

Hi all,
I'm trying to create a program that writes multiple lines of input to a file. However, for some reason, so far I've only been able to write the last line (which is "Close", because that's what stops the loop) to a file. How can I set this up so that that dosen't happen without using hundreds of thousands of variables?
Sorry... I thought I posted it. Here you go:
Graphics 1024,768
save$ = Input ("Under what name do you want this log saved?")
Writef$ = Input ("Ready.")
Repeat
mainfile$ = Input ("")
rcon = WriteFile("C:\temp\Test Docs"+save$+".con")
Until mainfile$ = "Close"
.writer
WriteLine (rcon,mainfile$)
CloseFile rcon
WaitKey 
End 

Thanks for you help.

We'll need to take a looky at your code... :)

Open the file (WriteFile()) before your loop starts - you only want to open it once. Write each line (WriteLine()) inside your loop.

P.S. Indent each line in a block (e.g. inside a loop) with a Tab character to improve readability.

Thank you all! This really helps!

*Edit*
Hello again,
I finished with the creation part of my program, but now I'm having trouble with the part for opening files. I'm getting the error "Stream Does Not Exist". Here's my code:
.start
Graphics 1024,768
open$ = Input ("Would you like to open or create a file? O = Open, C = Create, Q = Quit.")
If open$ = "O" Then Goto openscript 
If open$ = "C" Then Goto createscript
If open$ = "Q" Then End 
.openscript
openf = Input ("Please type the file you wish to open.")
fopen = OpenFile ("C:\temp\Test Docs"+open$+".rcn")
filetext= ReadFile (fopen)
fileread$ = ReadLine (filetext)
Print fileread$
WaitKey 
readf$ = Input ("Ready.")
Repeat
mainfile2$ = Input ("")
WriteLine (openf,mainfile2$)
Until mainfile2$ = "Close"
CloseFile fopen
.createscript
save$ = Input ("Under what name do you want this log saved?")
Writef$ = Input ("Ready.")
rcon = WriteFile("C:\temp\Test Docs"+save$+".rcn")
Repeat
WriteLine (rcon,mainfile$)
mainfile$ = Input ("")
Until mainfile$ = "Close"
WriteLine (rcon,mainfile$)
CloseFile rcon
WaitKey 
Goto start


hmm I would use local-folder addresses rather than start at the top of the tree drive letters.


Try just reading and writing to a file within the same folder as the .bb first

Now I'm getting "Memory Acess Violations". I haven't changed anything...

Maybe it's the Stream Does Not Exist error, but it's referred to as Memory Access Violation due to the Debugger being turned off?

Well, this bit's wrong, for starters:
fopen = OpenFile ("C:\temp\Test Docs"+open$+".rcn")
filetext= ReadFile (fopen)

That opens the specified file and assigns the file handle to fopen; you then use this file handle as the name to open another file for reading (ReadFile).

And the OpenFile line should be using the string variable openf$, not open$.

@Malice: Yeah, you were right about the debug thing. It's giving me the stream error now.
@big10p: I fixed the open$ error, thanks. But what should I use instead of ReadFile?

Try this, Mr.Bob94:
Graphics 1024,768

.start
open$ = Input ("Would you like to open or create a file? O = Open, C = Create, Q = Quit.")
If open$ = "O" Then Goto openscript 
If open$ = "C" Then Goto createscript
If open$ = "Q" Then End 

.openscript
openf$ = Input ("Please type the file you wish to open.")
fopen = OpenFile ("C:\temp\Test Docs"+openf$+".rcn")

While Not Eof(fopen)
	Print ReadLine (fopen)
Wend

readf$ = Input ("Ready.")

Repeat
	mainfile2$ = Input ("")
	WriteLine (fopen,mainfile2$)
Until mainfile2$ = "Close"

CloseFile fopen
WaitKey
Goto start

.createscript
save$ = Input ("Under what name do you want this log saved?")
rcon = WriteFile("C:\temp\Test Docs"+save$+".rcn")

Writef$ = Input ("Ready.")

Repeat
	mainfile$ = Input ("")
	WriteLine (rcon,mainfile$)
Until mainfile$ = "Close"

CloseFile rcon
WaitKey 
Goto start


Ok, I changed my sript to that, but I'm still getting the error. Here's my code now:
.start
Graphics 1024,768
open$ = Input ("Would you like to open or create a console session? O = Open, C = Create, Q = Quit.")
If open$ = "O" Then Goto openscript 
If open$ = "C" Then Goto createscript
If open$ = "Q" Then End 
.openscript
openf$ = Input ("Please type the file you wish to open.")
fopen = OpenFile ("\Documents"+openf$+".rcn")
While Not Eof(fopen)
	Print ReadLine (fopen)
Wend
readf$ = Input ("Ready.")
Repeat
	mainfile2$ = Input ("")
	WriteLine (fopen,mainfile2$)
Until mainfile2$ = "Close"

CloseFile fopen
WaitKey
Goto start
.createscript
save$ = Input ("Under what name do you want this log saved?")
Writef$ = Input ("Rynet Console Loaded.")
rcon = WriteFile("C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents"+save$+".rcn")
Repeat
WriteLine (rcon,mainfile$)
mainfile$ = Input ("")
Until mainfile$ = "Close"
	WriteLine (rcon,mainfile$)
	CloseFile rcon
WaitKey 
Goto start


Thanks again for all your help. :)

This bit looks like an error to me:


Until mainfile$ = "Close"
	WriteLine (rcon,mainfile$)
	CloseFile rcon
WaitKey



look at big10p's code

No, that part works fine for me. It's just in the Open File part that I'm getting errors.

Try...

fopen = OpenFile ("C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents"+openf$+".rcn")
If fopen=0 Then 
	Print "Unable to open logfile"
 	Goto openscript
EndIf

To see why the change to an abolute path might be relevant:
Print SystemProperty("appdir")
Relative paths from your program will work when you create an executable.
rcon = WriteFile("C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents"+save$+".rcn")
If rcon=0 Then
	Print "Unable to create logfile"
	Goto createscript
EndIf


I can't use the "Print SytemProperty("appdir")" command, it gives me a "Function Not Found" error.
As for your suggestion, that does stop the error, but I still can't read and then write to the file, and that's what I'm trying to do.

Thanks for the help.

Sorry, typo - corrected. It is SystemProperty().

If you want to append to a file check out the Filesize() and Seekfile() functions. They might be helpful.

Thanks. This is interesting, but I really could use some help with getting the files to open. I don't mean to push, it's just that I need to get this working soon.

It seems to work ok here with a couple of changes.

Graphics 1024,768

dirpath$="C:\Documents and Settings\Max Zimon\My Documents\IDL Files\Programs\Documents"
;dirpath$=SystemProperty("appdir") ; Use when creating executable
extension$=".rcn"
displayloginfo=True
tabspacing=4

Repeat
	Cls :	Locate 0,0 : Color 255,255,0
	open$ = Input ("Would you like to open or create a console session? O = Open, C = Create, Q = Quit.")
	Select Upper(open$)
		Case "O" Gosub Openscript
		Case "C" Gosub Createscript
		Case "Q" End
	End Select
Until False

.openscript
	Color 255,255,0
	openf$ = Input ("Please enter the file you wish to open.")

	If openf$="" Then Return

	ftype=FileType(dirpath$+openf$+extension$)

	If ftype=0 Then
		Color 255,0,0
		Print "file "+dirpath$+openf$+extension$+" does not exist. Please try again"
		Goto openscript
	Else
	
		Fsize=FileSize(dirpath$+openf$+extension$)
	
		fopen = OpenFile (dirpath+openf$+extension$)
		If fopen=0 Then 
			Color 255,0,0
			Print "Unable to open file for reading and writing"
		 	Goto openscript
		EndIf
		
		Gosub Readfilescript
	
		SeekFile fopen,fsize
	
	EndIf
	
	Print
	Color 255,255,0
	confirm$=Input ("Append to file (Y/N) ")
	
	If Upper(confirm$)<>"Y" Then Return

	Gosub WriteFileScript

Return


.CreateScript
	Color 255,255,0
	save$ = Input ("Enter log file name:")
	If save$="" Then Return
	fopen = WriteFile(dirpath+save$+extension$)
	If fopen=0 Then
		Color 255,0,0
		Print "Failed to create file"
		Goto CreateScript
	EndIf
	Gosub WriteFileScript
Return


.WriteFileScript
	Color 255,255,0
	Print "Input new data. Enter CLOSE to end"
	output$ = "*** Logging commenced on "+CurrentDate()+" at "+CurrentTime()
	Repeat
		WriteLine (fopen,output$)
		output$ = Input (">")
	Until Upper(output$) = "CLOSE"
	WriteLine (fopen,"*** Logging ended on "+CurrentDate()+" at "+CurrentTime())
	CloseFile fopen
Return

.ReadFileScript
	linecount=0
	maxlines=(GraphicsHeight()/FontHeight())-2	
	
	While Not Eof(fopen) Or KeyDown(1)
		fileinput$ = ReadLine(fopen)
		fileinput$ = Replace(fileinput$,Chr(9),String(" ",tabspacing))
		If Left(fileinput$,3)="***" Then 
			If Displayloginfo Then 
				Color 128,128,128 
				Print Right("0000"+linecount,5)+" "+fileinput$
				linecount=linecount+1
			EndIf
		Else
			Color 255,255,255
			Print Right("0000"+linecount,5)+" "+fileinput$
			linecount=linecount+1
		EndIf	
				
		If linecount Mod maxlines = 0  Then
			Color 255,255,0
			Print "(paused) Press any key"
			WaitKey()
		EndIf
	Wend
Return


That's amazing! Wow!! Thank you so much, it works perfectly!