Function Code Works Differant

BlitzMax Forums/BlitzMax Beginners Area/Function Code Works Differant

RedWizzard(Posted 2015) [#1]
Hi all,

I have a Queastion about a function code to write lines/strings to a file
I had setup my code in a test file first to write strings to a "log" file

When i changed the code in to a function its not working properly
i used sevral teqniqeus whit all the same bad result

(writeline,writestring,savestring ect)

at first it would only write on the first line of the txt file
ending whit the last string in putted in the function

i load/create the file first

Local DIR$=CurrentDir()
'Load Fix Log file
If Not OpenFile(DIR$+"/Log.txt")
CreateFile(DIR$+"/LOG.txt")
Print "cannot open log txt"
Print "Creating new Log file"
CreateFile(DIR$+"/LOG.txt")
Else Print "log succes"
EndIf
Local Wfile$=DIR$+"/LOG.txt"


Next i have added a funtion for writing line,
at start i had only a few lines for open stream,write,close
I ende up whit this

'new log function
Function WriteLog(Lt$,FileA$)
Print "LineW     "+Lt$+"FILE=    "+FileA$
Local File:TStream = OpenStream (FileA$)
Local LDS$ = LoadText(File:TStream)
WriteLine(File:TStream,Lt$)
Local SaveText (FileA$,File:TStream)
Local CloseStream(FileA$)
End Function


The strange thing is, when dont use the function
and just put in the code in my main file
loading the txt and using writeline it works like it should
displaying multiply linese of text in the file

I realy want to use functions since i want to write/load more data to/from files
so i wonder what am i doing wrong?
is there a certan way i need to change code to work good in a function?


Brucey(Posted 2015) [#2]
Based on your example code for your function, you may want to study some more examples of BlitzMax code, as it appears you don't understand a lot.

OpenStream() opens a stream, returning a stream handle.
LoadText() loads the whole file, using the stream handle file into a string. closes the stream on completion.
WriteLine() writes to the stream handle (which is closed). Will possibly crash at this point, or throw an exception.
SaveText() saves a whole string to a file (handle), replacing everything. Using a bad/closed stream handle may throw another exception.
CloseStream() takes a stream as an argument. That shouldn't even compile!?

Does your program even compile?
You should really use (at least) Strict or SuperStrict to help with your variable usage.

Also, in your original example, note that CreateFile() returns a TStream handle for you to use to write to it (using WriteLine() and friends).

If you want to append to the end of a file, you can do something like this :
Local stream:TStream = OpenStream(filename)
stream.Seek(stream.Size()) ' seek to the end of the file.

... do all your output, like
WriteLine(.....)

stream.Close()

By default, OpenStream() opens the file for reading and writing, which allows you to append stuff to the end of it.
Use Seek() to get the current position to the end of the file (otherwise you would be writing over existing data).


col(Posted 2015) [#3]
Hiya,

With files there are things known as a 'file position pointer'. This pointer is internal in the filing system as part of the OS. The file pointer is a pointer to where abouts in the file that any reading and writing should take place from. When you open a file the file pointer will usually always be pointing at the very beginning of the file. Any reading/writing to the file will move the file pointer to the end position after that last read/write operation.

The reason your code will work outside of a function is because you open the file once, write to it many times, then close the file. In the function you open the file, write once then close the file. The next you open it the file pointer is pointing to the beginning of the file which is what is causing your issue.

You can get around it by placing a SeekStream(File,StreamSize(File)) command to seek to the end of the file after you have opened each time, then any additions to the file will work as expected.

Another method would be to leave the file open at the start of your code, write to it when needed during code execution, and then close it when the app would close. This can have its own problems depending on the structure of your code and the bigger picture of what you want to achieve. To start with though and get your moving forward with what you have then use the SeekStream described above.


RedWizzard(Posted 2015) [#4]
Thanks for the fast reply,

I changed the function using the file.seek
I finnaly works thanks a lot!

I havent been programming for long just a few times before.
just getting the hang of the function/object stuff
i want my game to be modable, so thats wy i need load/writing to external files

I use strict in my main file, but not in my function file.
setting strict will not compile due to "labes must apearr before a loop or defdata statment"
il better chek the correct syntax for this function.

i did not know that createfile() retuns a tstream handle since that is not mentiont in the help file.
Greetings Gijs de mik


Brucey(Posted 2015) [#5]
My mistake, that's CreateStream() not CreateFile().
I usually just use the Stream-related functions if I'm creating and populating files.


RedWizzard(Posted 2015) [#6]
Strict works now
thougt i should declare external files as in C headers
using a #funct for refrance removed it works perfect in strict now

Function WriteLog(Lt:String,FileA:String)
	Print "LineW     "+Lt$+ "FILE=    "+FileA$
	Local File:TStream = OpenStream (FileA$)
	File.Seek(File.Size())
	WriteLine(File:TStream,Lt$)
	file.close()
End Function