l3ill Posted May 26, 2010 Share Posted May 26, 2010 (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 May 27, 2010 by billo My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
taietel Posted May 26, 2010 Share Posted May 26, 2010 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? ) Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
JohnOne Posted May 26, 2010 Share Posted May 26, 2010 I think you either need these files inside your function Local $file1 = "test1" Local $file2 = "test2" Local $file3 = "test3" Or make the Global AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
taietel Posted May 26, 2010 Share Posted May 26, 2010 It's working, even those variables are local. The problem was with GuiCtrlRead. The variables were not controlIDs, in his example. Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
l3ill Posted May 26, 2010 Author Share Posted May 26, 2010 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. expandcollapse popup#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 My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
taietel Posted May 27, 2010 Share Posted May 27, 2010 (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 Edited May 27, 2010 by taietel Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
Yoriz Posted May 27, 2010 Share Posted May 27, 2010 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. Link to comment Share on other sites More sharing options...
l3ill Posted May 27, 2010 Author Share Posted May 27, 2010 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 ??? expandcollapse popup#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 My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
taietel Posted May 27, 2010 Share Posted May 27, 2010 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! Things you should know first...In the beginning there was only ONE! And zero... Progs: Create PDF(TXT2PDF,IMG2PDF) 3D Bar Graph DeskGadget Menu INI Photo Mosaic 3D Text Link to comment Share on other sites More sharing options...
MHz Posted May 27, 2010 Share Posted May 27, 2010 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. expandcollapse popup#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 Link to comment Share on other sites More sharing options...
l3ill Posted May 27, 2010 Author Share Posted May 27, 2010 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 My Contributions... SnippetBrowser NewSciTE PathFinder Text File Manipulation FTP Connection Tester / INI File - Read, Write, Save & Load Example Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now