Sign in to follow this  
Followers 0
l3ill

FileWriteLine not working inside a Func

11 posts in this topic

#1 ·  Posted (edited)

Hi All,

Does anybody see why this code would work fine until I try to use it as a Function? With the Function code around it, it will not write to the file...

#include  Local $file1 = "test1" Local $file2 = "test2" Local $file3 = "test3" _test() Func _test()     ;MsgBox(4096, "Debug", "Function was Called") $file = FileOpen(@ScriptDir & "test.txt", 2) If $file = -1 Then   MsgBox(0, "Error", "Unable to open file.")  Exit EndIf FileWriteLine($file, GUICtrlRead($file1)) FileWriteLine($file, GUICtrlRead($file2)) FileWriteLine($file, GUICtrlRead($file3)) ConsoleWrite("$file1 = " & $file1 & @CRLF) FileClose($file) EndFunc

Thanks, Bill

Edited by billo

Share this post


Link to post
Share on other sites



It's not working because of this

$file = FileOpen(@ScriptDir & "\test.txt", 2)

(you forgot to put "\")

and these

FileWriteLine($file, $file1)
FileWriteLine($file, $file2)
FileWriteLine($file, $file3)

(you've tried to read what? :mellow: )

Share this post


Link to post
Share on other sites

Thanks for the speedy reply!

"/" this was wrong in the example but not in the main script and the GUICtrlRead was working in below code until I started splitting it up into Func.

Now it's not working and I cant find the worm.

So basically this code works if I take the funcs out and run it straight through.

#include <GUIConstantsEx.au3>
#include <File.au3>
Global $file0, $file, $file1, $file2, $file3, $r_file1, $r_file2, $r_file3, $msg, $btn

_main()
Func _main()
    $file = FileOpen("test.txt")
    If $file = -1 Then
        _createtxt()
    Else
        _ftpgui()
        Return 0
    EndIf
    $r_file1 = FileReadLine($file, 1)
    $r_file2 = FileReadLine($file, 2)
    $r_file3 = FileReadLine($file, 3)
    FileClose($file)
EndFunc ;==>_main

Func _createtxt()
    _FileCreate("test.txt")
    _main()
EndFunc ;==>_createtxt

Func _ftpgui()
    GUICreate("FTP Server Login Info", 350, 160, @DesktopWidth / 2 - 175, @DesktopHeight / 2 - 160, -1, 0x00000018); WS_EX_ACCEPTFILES
    GUICtrlCreateLabel("Server:", 10, 10)
    $file1 = GUICtrlCreateInput("", 100, 10, 240, 20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetData(-1, $r_file1)
    GUICtrlCreateLabel("Username:", 10, 40)
    $file2 = GUICtrlCreateInput("", 100, 40, 240, 20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetData(-1, $r_file2)
    GUICtrlCreateLabel("Password:", 10, 70)
    $file3 = GUICtrlCreateInput("", 100, 70, 240, 20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetData(-1, $r_file3)
    $btn = GUICtrlCreateButton("Ok", 145, 110, 60, 20)
    GUISetState()

    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
                _write()
                ExitLoop
        EndSelect
    WEnd
EndFunc ;==>_ftpgui

Func _write()

    $file = FileOpen("test.txt")
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

FileWriteLine($file, GUICtrlRead($file1))
FileWriteLine($file, GUICtrlRead($file2))
FileWriteLine($file, GUICtrlRead($file3))


    ConsoleWrite("$file1 = " & $file1 & @CRLF)

    FileClose($file)
EndFunc ;==>_write

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

The function _main() is wrong, because you set the data $file1, $file2, $file3 after you assign to them the values from the text file. It should be

Func _main()
         $file = FileOpen(@ScriptDir & "\test.txt")
         If $file = -1 Then
                   _createtxt()
         Else
               $r_file1 = FileReadLine($file, 1)
               $r_file2 = FileReadLine($file, 2)
               $r_file3 = FileReadLine($file, 3)
               _ftpgui()
                 Return 0
        EndIf
              FileClose($file)
      EndFunc ;==>_main

  :mellow:

Edited by taietel

Share this post


Link to post
Share on other sites

Heya, in your Func _write() you are using FileOpen in '0 = Read mode (default)' and then trying to 'FileWriteLine'.

You will need to use the mode parameter as either

1 = Write mode (append to end of file) or 2 = Write mode (erase previous contents) to anable writing to the file.


GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.

Share this post


Link to post
Share on other sites

Yup that was it..makes sense now.

@Yoriz, yeah I had just removed those before posting to see if they might be causing problems and forgot to put them back.

So now the top works but when I try to click ok / save the new data it wont open the txt file ???:mellow:

#include <GUIConstantsEx.au3>
#include <File.au3>
Global $file0, $file, $file1, $file2, $file3, $r_file1, $r_file2, $r_file3, $msg, $btn

_main()
Func _main()
    $file = FileOpen("test.txt", 0)
    If $file = -1 Then
        _createtxt()
    Else
        $r_file1 = FileReadLine($file, 1)
        $r_file2 = FileReadLine($file, 2)
        $r_file3 = FileReadLine($file, 3)
        _ftpgui()
        Return 0
    EndIf
    FileClose($file)
EndFunc ;==>_main

Func _createtxt()
    _FileCreate("test.txt")
    _main()
EndFunc ;==>_createtxt

Func _ftpgui()
    GUICreate("FTP Server Login Info", 350, 160, @DesktopWidth / 2 - 175, @DesktopHeight / 2 - 160, -1, 0x00000018); WS_EX_ACCEPTFILES
    GUICtrlCreateLabel("Server:", 10, 10)
    $file1 = GUICtrlCreateInput("", 100, 10, 240, 20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetData(-1, $r_file1)
    GUICtrlCreateLabel("Username:", 10, 40)
    $file2 = GUICtrlCreateInput("", 100, 40, 240, 20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetData(-1, $r_file2)
    GUICtrlCreateLabel("Password:", 10, 70)
    $file3 = GUICtrlCreateInput("", 100, 70, 240, 20)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)
    GUICtrlSetData(-1, $r_file3)
    $btn = GUICtrlCreateButton("Ok", 145, 110, 60, 20)
    GUISetState()

    $msg = 0
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
                _write()
                ExitLoop
        EndSelect
    WEnd
EndFunc ;==>_ftpgui

Func _write()

    $file = FileOpen("test.txt", 2)
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf

    FileWriteLine($file, GUICtrlRead($file1))
    FileWriteLine($file, GUICtrlRead($file2))
    FileWriteLine($file, GUICtrlRead($file3))

    FileClose($file)
EndFunc ;==>_write

Share this post


Link to post
Share on other sites

So now the top works but when I try to click ok / save the new data it wont open the txt file ???

Open, or write new data?

I've checked and the data is saved in the text file! If you want to open it after that, to see if the data is there, just insert the proper code or use notepad! :mellow:

Share this post


Link to post
Share on other sites

These lines below should indicate the issue. Bolded the variable that is a Global problem.

...

Global $file0, $file, ...

...

$file = FileOpen("test.txt", 0)

...

$file = FileOpen("test.txt", 2)

...

If you want to use the same variable name in different functions for different uses then you may need to make them local. I would recommend different and more descriptive names for those variables but that is something for you to consider.

Since you keep changing your code, then I changed it some too. This may help give you another view of doing it.

#include <GUIConstantsEx.au3>
#include <File.au3>
Global $file0, $file1, $file2, $file3, $r_file1, $r_file2, $r_file3

_main()
Func _main()
    ; commented variables in declaration are reminder of being Global/Global Const
    Local $file; $r_file1, $r_file2, $r_file3
    If Not FileExists("test.txt") Then
        _createtxt()
    Else
        $file = FileOpen("test.txt", 0)
        If $file <> -1 Then
            $r_file1 = FileReadLine($file, 1)
            $r_file2 = FileReadLine($file, 2)
            $r_file3 = FileReadLine($file, 3)
            FileClose($file)
        EndIf
        _ftpgui()
        ; exit if @error is not 0
        If @error Then
            Exit 1
        EndIf
    EndIf
EndFunc ;==>_main

Func _createtxt()
    _FileCreate("test.txt")
    _main()
EndFunc ;==>_createtxt

Func _ftpgui()
    Local $btn, $msg; $file1, $file2, $file3, $GUI_DROPACCEPTED, $GUI_EVENT_CLOSE
    GUICreate("FTP Server Login Info", 350, 160, @DesktopWidth / 2 - 175, @DesktopHeight / 2 - 160, Default, 0x00000018); WS_EX_ACCEPTFILES
    GUICtrlCreateLabel("Server:", 10, 10)
    $file1 = GUICtrlCreateInput("", 100, 10, 240, 20)
    GUICtrlSetState(Default, $GUI_DROPACCEPTED)
    GUICtrlSetData(Default, $r_file1)
    GUICtrlCreateLabel("Username:", 10, 40)
    $file2 = GUICtrlCreateInput("", 100, 40, 240, 20)
    GUICtrlSetState(Default, $GUI_DROPACCEPTED)
    GUICtrlSetData(Default, $r_file2)
    GUICtrlCreateLabel("Password:", 10, 70)
    $file3 = GUICtrlCreateInput("", 100, 70, 240, 20)
    GUICtrlSetState(Default, $GUI_DROPACCEPTED)
    GUICtrlSetData(Default, $r_file3)
    $btn = GUICtrlCreateButton("Ok", 145, 110, 60, 20)
    GUISetState()
    ;
    While $msg <> $GUI_EVENT_CLOSE
        $msg = GUIGetMsg()
        Select
            Case $msg = $btn
                _write()
                ; check for fileopen error below
                If @error = 1 Then
                    MsgBox(0, "Error", "Unable to open file for write.")
                    ; return @error to main()
                    Return SetError(@error, 0, 0)
                EndIf
                ExitLoop
        EndSelect
    WEnd
EndFunc ;==>_ftpgui

Func _write()
    Local $file; $file1, $file2, $file3
    $file = FileOpen("test.txt", 2)
    If $file = -1 Then
        ; return @error = 1, @extended = 2 (fileopen mode used), value = empty string
        Return SetError(1, 2, '')
    EndIf
    ;
    FileWriteLine($file, GUICtrlRead($file1))
    FileWriteLine($file, GUICtrlRead($file2))
    FileWriteLine($file, GUICtrlRead($file3))
    ;
    FileClose($file)
EndFunc ;==>_write

:mellow:

Share this post


Link to post
Share on other sites

Hi Again,

@ Taitel: it wasn't opening, I was getting the built in popup error msgbox "Unable to open file."

@MHZ: LOL you code changer you...as long as it's better:lol: actually I had played with the idea of two separate vars for "$File" as you can see in your quote, "File0". but then it started getting buggy so I changed it back again. BTW thanks for the well commented example!

So it looks like I need more practice with Global Local and Constant and when to use what.

Thanks again everybody for your input...see ya around!!

Bill

Share this post


Link to post
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
Sign in to follow this  
Followers 0