OpenFile() Bug or misunderstanding?

BlitzMax Forums/BlitzMax Programming/OpenFile() Bug or misunderstanding?

TomToad(Posted 2008) [#1]
I have found what I think is a bug with the OpenFile() command. I was under the impression that when OpenFile() is called with the write flag = true, that it will create the file if it doesn't exist. However, I get an error.
SuperStrict

Local File:TStream = OpenFile("MyTestFile.Test",True,True)
If Not File Then RuntimeError "Couldn't open MyTestFile.Test"

CloseFile File

When I create the file first, then the file will open.
SuperStrict

CreateFile("MyTestFile.Test")
Local File:TStream = OpenFile("MyTestFile.Test",True,True)
If Not File Then RuntimeError "Couldn't open MyTestFile.Test"

CloseFile File

Is this a bug or did I just misunderstand how OpenFile() works?


Jesse(Posted 2008) [#2]

Function OpenFile:TStream( url:Object,readable=True,writeable=True )

Description Open a file for input and/or output.
Information This command is similar to the OpenStream command but will attempt to cache the contents of the file to ensure serial streams such as http: based url's are seekable. Use the CloseStream command when finished reading and or writing to a Stream returned by OpenFile.
Example ' openfile.bmx

' the following prints the contents of this source file

file=openfile("openfile.bmx")

if not file runtimeerror "could not open file openfile.bmx"

while not eof(file)
print readline(file)
wend
closestream file





No where does it say that the file will be created. The flags are used for modification purpose only.


Koriolis(Posted 2008) [#3]
Not quite true. It does create the file ... sometimes.
I have looked at the OpenStream code, and it all boils down to what TCstream.OpenStream does : when you specify both readable and writeable, it will NOT wreate the file, but if you specify only writable, it will.
It may be considered a defect, if it creates in write mode it would probably make sense to create too in write&read mode (all there is to do is to change "wb" to "w+b" in TCStream.OpenFile).
However I imagine that it was done on purpose: by doing this it would be too easy to overwrite an existing file as OpenFile will by default open in both read and write mode.
Though in my opinion the right fix would be to:
1) change "wb" to "w+b" so that in read+write mode the file is created
2) in OpenFile, change the default value of the "writeable" parameter to false (instead of true)