OPENing files.. We need an option..

General FreeBASIC programming questions.
Aqua32
Posts: 49
Joined: Jun 16, 2005 6:38

OPENing files.. We need an option..

Post by Aqua32 »

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?
Chronos
Posts: 40
Joined: May 31, 2005 15:21
Location: KY, USA
Contact:

Post by Chronos »

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.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

You mean BINARY or RANDOM mode?
mjs
Site Admin
Posts: 842
Joined: Jun 16, 2005 19:50
Location: Germany

Re: OPENing files.. We need an option..

Post by mjs »

Aqua32 wrote:OPEN "file.ext" for binary NEW as #f
You probably want to try this:

Code: Select all

OPEN "file.ext" FOR BINARY ACCESS READ AS #f
This will cause an error when:
  • the file doen't exist
  • you compiled the application with -e (or -ex or -exx)
But be warned that you cannot write to this file. You can only read from it.

Regards,
Mark
Chronos
Posts: 40
Joined: May 31, 2005 15:21
Location: KY, USA
Contact:

Post by Chronos »

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...

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
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.
rdc
Posts: 1745
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

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
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Post by Sisophon2001 »

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
Z!re

Post by Z!re »

[Content removed at author's request]
rdc
Posts: 1745
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

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
I am not sure what you mean here. This code works fine, in Windows at least.

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
This successfully differeniates between a folder and file. As Z!ire said, you can't have both a folder and file with the same name, in Windows at least. So I don't see the problem.
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

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.
Aqua32
Posts: 49
Joined: Jun 16, 2005 6:38

Post by Aqua32 »

I agree with jofers.. But behind the scenes it would still be doing the same thing as:

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..
Reason?
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.
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.
mjs
Site Admin
Posts: 842
Joined: Jun 16, 2005 19:50
Location: Germany

Post by mjs »

It does always return a status code - but sometimes, OPEN simply doesn't fail when you use the "wrong" OPEN arguments *g*.

Regards,
Mark
rdc
Posts: 1745
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

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.
Well, if you want to go that route, then just use CreateFile in kernel32.bi (OpenFile is for 16-bit systems).
dwDesiredAccess
[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.
http://msdn.microsoft.com/library/defau ... tefile.asp
Aqua32
Posts: 49
Joined: Jun 16, 2005 6:38

Post by Aqua32 »

Whew. Thats quite a long winded function there.. :D

Thanks.
rdc
Posts: 1745
Joined: May 27, 2005 17:22
Location: Texas, USA
Contact:

Post by rdc »

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
Post Reply