Page 1 of 1 1
Topic Options
#161977 - 2006-05-17 01:22 AM File Handle questions
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
When you open a file is the filehandle a global or local definition.

I.E. you open a file in a function, can you then do a writeline in another function using the proper file handle?
_________________________
Today is the tomorrow you worried about yesterday.

Top
#161978 - 2006-05-17 01:45 AM Re: File Handle questions
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Without trying, I would say most definitely yes - file handles are internal to kixtart and dont have scope - they would be "global" by default.
Top
#161979 - 2006-05-17 06:10 AM Re: File Handle questions
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Garg,

Not sure if you'v seen this or not as not everyone has read the entire manual and not too many have memorized it but using this method should prevent any file handle issue.

Code:
FreeFileHandle( )

Action: Returns the first available file handle.

Syntax: FreeFileHandle ( )

Parameters: None.

Returns: 0 No file handle available.
> 0 Numeric file handle.


See Also: Open( ), Close( ), ReadLine( ), WriteLine( )

Example: $Handle = FreeFileHandle()
IF $Handle > 0
IF Open($Handle, @LDRIVE + "\CONFIG\SETTINGS.INI") = 0
;………
ENDIF
ENDIF



Top
#161980 - 2006-05-17 08:38 AM Re: File Handle questions
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Yes I use that method all of the time. My question was more to the fact of ...

Code:

Function OpenFile()
Open (1,"c:\config.sys",5)
EndFunction

Function Write2File($Handle)
Writeline(1, "This is a test")
EndFunction



Where a better way probably would be....
Code:

$FH = FreeFileHandle()

Function OpenFile($FH)
Open ($FH,"C:\Config.sys",5)
EndFunction

Function Write2File ($FH)
Writeline ($FH,"Nobody uses this file anymore")
EndFunction

_________________________
Today is the tomorrow you worried about yesterday.

Top
#161981 - 2006-05-17 09:32 AM Re: File Handle questions
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
don't forget to close the file properly too.
Top
#161982 - 2006-05-17 12:39 PM Re: File Handle questions
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
be careful when using FreeFileHandle() - don't get caught out by this schoolboy error:
Code:
$fhLogFile=FreeFileHandle()
$fhOutFile=FreeFileHandle()

; Open log file...
If Open($fhLogFile,"c:\temp\log.txt",5) "CANNOT OPEN LOG FILE"+@CRLF Exit @ERROR EndIf

; Open out file...
If Open($fhOutFile,"c:\temp\out.txt",5) "CANNOT OPEN OUT FILE"+@CRLF Exit @ERROR EndIf


Top
#161983 - 2006-05-17 02:24 PM Re: File Handle questions
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Richard, you should probably explain in greater detail why that doesn't work.
Top
#161984 - 2006-05-17 08:03 PM Re: File Handle questions
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
Quote:

be careful when using FreeFileHandle() - don't get caught out by this schoolboy error:
Code:
$fhLogFile=FreeFileHandle()
$fhOutFile=FreeFileHandle()

; Open log file...
If Open($fhLogFile,"c:\temp\log.txt",5) "CANNOT OPEN LOG FILE"+@CRLF Exit @ERROR EndIf

; Open out file...
If Open($fhOutFile,"c:\temp\out.txt",5) "CANNOT OPEN OUT FILE"+@CRLF Exit @ERROR EndIf






Though perhaps not as obvious to some it would not work as Richard wrote it.

BOTH files would have a FreeFile number of 1 which would then cause an error.
$fhLogFile=FreeFileHandle()
$fhOutFile=FreeFileHandle()

Since the call is one right after the other without actually opening any file, they
will both end up having the same value for their var.
 
I would probably close the files in each of the IF blocks, but as in this example it could be closed at the end if you like..

Dim $fhLogFile, $fhOutFile, $RC
; Open log file...
$fhLogFile=FreeFileHandle()
'Value for FREE LOG is: ' + $fhLogFile ?
If Open($fhLogFile,"c:\temp\log.txt",5)=0
$RC=WriteLine($fhLogFile,'Wrting to LOG file okay'+@CRLF+'Freehandle is: '+$fhLogFile)
Else
"CANNOT OPEN LOG FILE"+@CRLF
Exit @ERROR
EndIf

; Open out file...
$fhOutFile=FreeFileHandle()
'Value for FREE OUT File is: ' + $fhOutFile ?
If Open($fhOutFile,"c:\temp\out.txt",5)=0
$RC=WriteLine($fhOutFile,'Wrting to OUT file okay'+@CRLF+'Freehandle is: '+$fhOutFile)
Else
"CANNOT OPEN OUT FILE"+@CRLF
Exit @ERROR
EndIf

$RC=Close($fhLogFile)
$RC=Close($fhOutFile)


Top
#161985 - 2006-05-17 11:17 PM Re: File Handle questions
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Thanks Doc, now that you say without opening a file, it would return the same value, it makes alot of sense. I geuss I have been lucky in that I only opened 1 file.

Great example.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#161986 - 2006-05-18 09:24 AM Re: File Handle questions
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
To close this one off, my preferred method is to create a simple wrapper for the Open() function. This probably also answers your original question about the scope of file handles too

Code:
Dim $fhLogFile, $fhOutFile

; Open log file...
$fhLogFile=udfOpenFile("C:\temp\log.txt",5)
If @ERROR "CANNOT OPEN LOG FILE"+@CRLF Exit @ERROR EndIf
; Open out file
$fhOutFile=udfOpenFile("C:\temp\Out.txt",5)
If @ERROR "CANNOT OPEN OUT FILE"+@CRLF Exit @ERROR EndIf

Function udfOpenFile($sPath,Optional $iMode)
If ""=$iMode $iMode=2 EndIf
$udfOpenFile=FreeFileHandle()
If @ERROR $udfOpenFile=0 Exit @ERROR EndIf
If Open($udfOpenFile,$sPath,$iMode) $udfOpenFile=0 Exit @ERROR EndIf
Exit @ERROR
EndFunction


Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (mole) and 1300 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.099 seconds in which 0.064 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org