Search the Community
Showing results for tags 'boolean'.
-
Here's the Help file example for the FileOpen function. #include <FileConstants.au3> #include <MsgBoxConstants.au3> Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = @TempDir & "\FileOpen.txt" ; Create a temporary file to read data from. If Not FileCreate($sFilePath, "This is an example of using FileOpen.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. Local $sFileRead = FileRead($hFileOpen) ; Close the handle returned by FileOpen. FileClose($hFileOpen) ; Display the contents of the file. MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead) ; Delete the temporary file. FileDelete($sFilePath) EndFunc ;==>Example ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreate In line 5 of the Func Example() there's a Return before MsgBox. Is the Return meant to exit the function? How is MsgBox executed with a Return before it? I haven't noticed this technique before, although I can see what you'd want to provide a way to abort if the temp file can't be created. If Not FileCreate($sFilePath, "This is an example of using FileOpen.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") Also, what is the reasoning and purpose behind the design of FileCreate function? ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreateI assume that the goal of the first line is to declare the local variable, $bReturn, and setting it to True is a bet that the file will be created successfully. The second line starts out understandable: "if the file does not already exist then create and write the string to it. But what's the " = 1" hanging off the end? I can't say I've ever seen a statement like "variable = function = value" before (even though this same FileCreate function is used in several Help file examples -- I just never noticed these details before). I'm going to need some hand holding through that one. Why was the FileCreate function even employed when the following seems adequate for creating and writing text to the target file in the Example function? ; Create a temporary file to read data from. If Not FileWrite ($sFilePath,"This is an example of using FileOpen.") then MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")I realize this leaves out checking to see if the file already exists but what will happen if FileWrite opens and writes text to an existing file?
-
Here's the Help file example for the FileOpen function. #include <FileConstants.au3> #include <MsgBoxConstants.au3> Example() Func Example() ; Create a constant variable in Local scope of the filepath that will be read/written to. Local Const $sFilePath = @TempDir & "\FileOpen.txt" ; Create a temporary file to read data from. If Not FileCreate($sFilePath, "This is an example of using FileOpen.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") ; Open the file for reading and store the handle to a variable. Local $hFileOpen = FileOpen($sFilePath, $FO_READ) If $hFileOpen = -1 Then MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.") Return False EndIf ; Read the contents of the file using the handle returned by FileOpen. Local $sFileRead = FileRead($hFileOpen) ; Close the handle returned by FileOpen. FileClose($hFileOpen) ; Display the contents of the file. MsgBox($MB_SYSTEMMODAL, "", "Contents of the file:" & @CRLF & $sFileRead) ; Delete the temporary file. FileDelete($sFilePath) EndFunc ;==>Example ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreate In line 5 of the Func Example() there's a Return before MsgBox. Is the Return meant to exit the function? How is MsgBox executed with a Return before it? I haven't noticed this technique before, although I can see what you'd want to provide a way to abort if the temp file can't be created. If Not FileCreate($sFilePath, "This is an example of using FileOpen.") Then Return MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.") Also, what is the reasoning and purpose behind the design of FileCreate function? ; Create a file. Func FileCreate($sFilePath, $sString) Local $bReturn = True ; Create a variable to store a boolean value. If FileExists($sFilePath) = 0 Then $bReturn = FileWrite($sFilePath, $sString) = 1 ; If FileWrite returned 1 this will be True otherwise False. Return $bReturn ; Return the boolean value of either True of False, depending on the return value of FileWrite. EndFunc ;==>FileCreateI assume that the goal of the first line is to declare the local variable, $bReturn, and setting it to True is a bet that the file will be created successfully. The second line starts out understandable: "if the file does not already exist then create and write the string to it. But what's the " = 1" hanging off the end? I can't say I've ever seen a statement like "variable = function = value" before (even though this same FileCreate function is used in several Help file examples -- I just never noticed these details before). I'm going to need some hand holding through that one. Why was the FileCreate function even employed when the following seems adequate for creating and writing text to the target file in the Example function? ; Create a temporary file to read data from. If Not FileWrite ($sFilePath,"This is an example of using FileOpen.") then MsgBox($MB_SYSTEMMODAL, "", "An error occurred whilst writing the temporary file.")I realize this leaves out checking to see if the file already exists but what will happen if FileWrite opens and writes text to an existing file?