Jump to content

FileWrite doesn't use suplied handle


RaySS
 Share

Recommended Posts

FileOpen does return the expected handle in the code below, but FileWrite doesn't write text lines to the newly opened file. Here's the code:

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Global $sTopDirectory = "D:\Clif\Test\", $sSaveTopDirectory, $hFilehandle, $hFilehandleINI, $sOutFile, $sDSS_log = "DSS.log", $sDSS_txt = "DSS.txt"
Global $aFilesFound[1] = [0], $sCreateTime = ""
Local $sEnvVar, $iLucky = 0, $3FitFail = 0, $i0000Fail = 0, $i1999Fail = 0, $i1999Warn = 0, $i9999Warn = 0, $iRefStar = 0, $iDoubleStar = 0, $iHitCount = 0


$sEnvVar = EnvGet("APPDATA")
$sFileNameINI = $sEnvVar & "\Doublestar\Structure.ini"

; Open file - preserving any existing content
$hFilehandleINI = FileOpen($sFileNameINI, $FO_APPEND)

; Prove it exists
If FileExists($sFileNameINI) Then
    MsgBox($MB_SYSTEMMODAL, "File", "Exists");Code to be supplied later
Else
    MsgBox($MB_SYSTEMMODAL, "File", "Does not exist")
    $hFilehandleINI = FileOpen($sFileNameINI, $FO_OVERWRITE) ;Get handle for %APPDATA%/Doublestar/Structure.ini
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sFileNameINI = ' & $sFileNameINI & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
    FileWrite($hFilehandleINI, "[TopDirectory]")
    FileWrite($hFilehandleINI, @CRLF & "TopDirectory=" & $sTopDirectory & @CRLF)
    ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hFilehandleINI = ' & $hFilehandleINI & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
EndIf

; Close the file handle
FileClose($hFilehandleINI)

Here's the console output:

@@ Debug(25) : $sFileNameINI = C:\Users\RAS\AppData\Roaming\Doublestar\Structure.ini
>Error code: 0
@@ Debug(28) : $hFilehandleINI = -1
>Error code: 0

The file name shown in line 25 above is correct, but I'm misusing the file handle in some way. Please tell me what I'm missing.

Thank you.

RaySS

Edited by RaySS
Link to comment
Share on other sites

I don't see the point of doing a FileOpen before FileExists.

You are also using FileOpen twice on the same file, without closing the first FileOpen.

I haven't tested your code, obviously, but the location you are writing to would have a Permissions element.

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

And you should be using the @error returned to see if FileOpen worked okay and for FileWrite, instead of bothering with FileExists, which could be used afterwards still if you wanted a double check.

$hFilehandleINI = FileOpen($sFileNameINI, $FO_APPEND)
If @error Then
     MsgBox($MB_SYSTEMMODAL, "File Error", "Does not exist and could not be created!")
Else
    ; your FileWrite code goes here
End If

P.S. I am presuming you have a reason for not using IniWrite etc?

Edited by Santa

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Link to comment
Share on other sites

Here lies the problem:

$sFileNameINI = $sEnvVar & "\Doublestar\Structure.ini" ; $sEnvVar is currently empty ("") <---- False

And \Doublestar\Structure.ini is not a valid file... I assume that there is no Doublestar folder in the @ScriptDir :P

$sEnvVar is set to %appdata%... Try this code:

ShellExecute($sEnvVar & "\Doublestar\Structure.ini")

Does this open Structure.ini? If not, then the path is invalid...

Edited by TheDcoder

EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time)

DcodingTheWeb Forum - Follow for updates and Join for discussion

Link to comment
Share on other sites

I had a fundamental misunderstanding of file I/O. The following are my reactions to your contributions. You will see my evolution.  


@Santa

I don't see the point of doing a FileOpen before FileExists.

In the program where this code will be running, I expect that the Structure.ini file will already have been written previously. If that file does exist, I will read from it and do further processing (not yet coded).


You are also using FileOpen twice on the same file, without closing the first FileOpen.
 

If control gets into line 24, the file does not exist, therefore, it will be appropriate to create it and open for writing with the $FO_OVERWRITE mode.


I haven't tested your code, obviously, but the location you are writing to would have a Permissions element.
 

What is a "permissions element"? Are you saying I need a FileClose($hFilehandleINI) statement immediately following line 22 in the ELSE block?

 

@ jguinch
Also, there are some native functions to work with INI files : IniWrite, IniRead...

Yes, I saw those, but decided to write my own. It will be a very short .INI file with only one brief section. If I can get this section to work, I may be using it for writing short memo files elsewhere.


@TheDcoder
Does this open Structure.ini? If not, then the path is invalid...

Yes, the path is invalid because the conditional on line 20 is false and sends control to the ELSE block at line 22.

 

@Santa (second reply)
And you should be using the @error returned to see if FileOpen worked okay ...

I see now that the $FO_APPEND mode will open the file even if it doesn't already exist. I had thought "append" implied the statement would fail if the file was not already present and filled with a minimum of something more than $var = Null.

Here's your code fragment which I'm using as a model. I tested it and it works.

#include <File.au3>
#include <Array.au3>
#include <FileConstants.au3>
#include <MsgBoxConstants.au3>

Global $sTopDirectory = "D:\Clif\Test\", $sSaveTopDirectory, $hFilehandle, $hFilehandleINI, $sOutFile, $sDSS_log = "DSS.log", $sDSS_txt = "DSS.txt"
Global $aFilesFound[1] = [0], $sCreateTime = ""
Local $sEnvVar, $iLucky = 0, $3FitFail = 0, $i0000Fail = 0, $i1999Fail = 0, $i1999Warn = 0, $i9999Warn = 0, $iRefStar = 0, $iDoubleStar = 0, $iHitCount = 0


$sEnvVar = EnvGet("APPDATA")
$sFileNameINI = $sEnvVar & "\Doublestar\Structure.ini"

$hFilehandleINI = FileOpen($sFileNameINI, $FO_APPEND)
If @error Then
    MsgBox($MB_SYSTEMMODAL, "File Error", "Does not exist and could not be created!")
Else
    MsgBox($MB_SYSTEMMODAL, "File Exists", "Success")
EndIf

; Close the file handle
FileClose($hFilehandleINI)

My thanks to all who replied.
RaySS

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...