OPENing files.. We need an option..
OPENing files.. We need an option..
We really need to be able to choose whether or not the file is created when attempting to open a file! Like so:
OPEN "file.ext" for binary NEW as #f
might open a file and on error create it.. If NEW is not specified, an error returns. That would be cool, yes?
OPEN "file.ext" for binary NEW as #f
might open a file and on error create it.. If NEW is not specified, an error returns. That would be cool, yes?
Only problem is that NEW would add another keyword to the list of reserved words, and "new" might have functions in the future for object manipulation when Object-Oriented Programming arrives. New might already be implemented--I'll check the Wiki on it.
Code: Select all
Dim Automobile as NEW Car
Last edited by Chronos on Oct 06, 2005 12:49, edited 1 time in total.
Re: OPENing files.. We need an option..
You probably want to try this:Aqua32 wrote:OPEN "file.ext" for binary NEW as #f
Code: Select all
OPEN "file.ext" FOR BINARY ACCESS READ AS #f
- the file doen't exist
- you compiled the application with -e (or -ex or -exx)
Regards,
Mark
A (rather sloppy) way of going about creating a binary file in the manner you're wanting could be done like this, I think... (I don't have FB available everywhere anymore so I can't test this):
Compile with -ex though...
All I'm using "for output" for is to create a blank file to write to. I believe this would work, but it is a bit sloppy as I mentioned before.
Addendum: NEW doesn't have any reserved status right now, but I'd expect it to when OOP comes around.
Compile with -ex though...
Code: Select all
ON ERROR GOTO panic
OPEN "file.ext" FOR BINARY ACCESS WRITE AS #1
'Actions
CLOSE #1
END
panic:
IF ERR = 2 THEN 'File Not Found
OPEN "file.ext" FOR OUTPUT AS #1
CLOSE #1
RESUME
Addendum: NEW doesn't have any reserved status right now, but I'd expect it to when OOP comes around.
An easier way is to simply use DIR to see if the file exists and then open or create it depending on if it exists or not. There is no need to add another keyword to OPEN.
Code: Select all
If len(dir$(filename.ext)) > 0 then
'open it
else
'create it
end if
-
- Posts: 1706
- Joined: May 27, 2005 6:34
- Location: Cambodia, Thailand, Lao, Ireland etc.
- Contact:
Hi:
Dir is unreliable in this usage. Your code will fail if there is a directory name with the same name as the file, or if the file is a system file.
The FB windows implementation of DIR is not useful without an attribute command. It can not tell the difference between a file with no attributes and a directory.
Garvan
Dir is unreliable in this usage. Your code will fail if there is a directory name with the same name as the file, or if the file is a system file.
The FB windows implementation of DIR is not useful without an attribute command. It can not tell the difference between a file with no attributes and a directory.
Garvan
I am not sure what you mean here. This code works fine, in Windows at least.Sisophon2001 wrote:Hi:
Dir is unreliable in this usage. Your code will fail if there is a directory name with the same name as the file, or if the file is a system file.
The FB windows implementation of DIR is not useful without an attribute command. It can not tell the difference between a file with no attributes and a directory.
Garvan
Code: Select all
If Len(Dir$("test", 16)) > 0 Then
Print "Directory"
ElseIf Len(Dir$("test")) > 0 Then
Print "File"
Else
Print "Not found"
End If
Sleep
Tokens are possible to have without reserving them. For instance, nothing prevents you from using "alpha" as a variable name. FreeBASIC has a pretty good lexer that can look ahead and identify class, text or id tokens.
But a much better solution would be to add a FileExists function to the runtime library.
But a much better solution would be to add a FileExists function to the runtime library.
I agree with jofers.. But behind the scenes it would still be doing the same thing as:
Reason?
Code: Select all
'Check if exists first..
res = open (fname for binary Access read as #f)
IF res <> 0 THEN
res = (res - res) - res' IMO error value should be neg if failed ;-)
close #f
EXIT FUNCTION
END IF
close #f
'then your open for write..
I think the FB syntax should be reworked. It should always return a value no matter what access, file type, open method, etc. OR add EXISTS to FB. Just my opinion.OpenFile API says:
uStyle - Specifies the action to take. The following values can be combined by using the bitwise OR operator:
OF_EXIST
Opens the file and then closes it. Used to test for a file’s existence.
Well, if you want to go that route, then just use CreateFile in kernel32.bi (OpenFile is for 16-bit systems).Aqua32 wrote:OpenFile API says:
uStyle - Specifies the action to take. The following values can be combined by using the bitwise OR operator:
OF_EXIST
Opens the file and then closes it. Used to test for a file’s existence.
http://msdn.microsoft.com/library/defau ... tefile.aspdwDesiredAccess
[in] The access to the object, which can be read, write, or both.
For more information, see File Security and Access Rights. You cannot request an access mode that conflicts with the sharing mode that is specified in an open request that has an open handle.
If this parameter is 0 (zero), the application can query file and device attributes without accessing a device. This is useful for an application to determine the size of a floppy disk drive and the formats it supports without requiring a floppy in a drive. It can also be used to test for the existence of a file or directory without opening them for read or write access.
Everything, including the kitchen sink is in there. :) It isn't too bad if you are just checking if a file exists. Here is a C example:
http://www.nirsoft.net/vc/isfileexist.html
http://www.nirsoft.net/vc/isfileexist.html