sambuddy Posted December 6, 2006 Share Posted December 6, 2006 Hello All, I'm trying to open a file and read it line by line but I keep getting told that I can't open it! Any idea why? Thanks Regards ;*****************make the text file************************************************* $PID=RunWait(@ComSpec & " /C hinfo.exe app.mux > hinfo.txt",$current_directory,@SW_HIDE) ProcessWaitClose($PID) ;************make sure the file exists before opening it********************************** $0=FileExists("hinfo.txt") GUICtrlSetData($txt1,GuiCtrlRead($txt1) & "File exist = " & $0 & @CRLF) while $0 = 0 $0=FileExists($current_directory & "\hinfo.txt") GUICtrlSetData($txt1,GuiCtrlRead($txt1) & "File doesn't exist" & @CRLF) ;Sleep(1000) WEnd ;*****************open the file************************************************* $txt=FileOpen("hinfo.txt",0) ;**************check that it opened correctly************************************** If $txt = -1 Then MsgBox(0, "Error", "Unable to open file.") ;Exit EndIf ;**************read file line at a time into an edit box****************************** While 1 $line = FileReadLine($txt) If @error = -1 Then ExitLoop GUICtrlSetData($txt1,GuiCtrlRead($txt1) & $line) WEnd Link to comment Share on other sites More sharing options...
Thatsgreat2345 Posted December 6, 2006 Share Posted December 6, 2006 learn about For Next loop For $i = 1 to _FileCountLines('hinfo.txt') GUICtrlSetData($txt1,GuiCtrlRead($txt1) & FileReadLine('hinfo.txt',$i)) Next Link to comment Share on other sites More sharing options...
Edgar Posted December 6, 2006 Share Posted December 6, 2006 (edited) I edit the code you posted and it works at my computer. For your reference . #include <GUIConstants.au3> GUICreate("TEST",200,300) $txt1 = GUICtrlCreateEdit("",0,0,180,300) GUISetState() $0=FileExists("hinfo.txt") If Not $0 Then MsgBox(0,"TEST", "File exist = " & $0 & @CRLF) Else $txt=FileOpen("hinfo.txt",0) If $txt <> -1 Then While 1 $line = FileReadLine($txt) If @error = -1 Then ExitLoop GUICtrlSetData($txt1,GuiCtrlRead($txt1) & $line) WEnd Else MsgBox(0, "Error", "Unable to open file.") EndIf EndIf While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop EndSelect WEnd Edited December 6, 2006 by Edgar Link to comment Share on other sites More sharing options...
sambuddy Posted December 6, 2006 Author Share Posted December 6, 2006 Thanks Edgar, that works a treat. I also tried the solution by "Thatsgreat2345" but i got the same result as i did with my code ( although i did notice that my text file appears to be all on one line, maybe that has an effect!) I will investigate further and thankyou for the solution I edit the code you posted and it works at my computer. For your reference . #include <GUIConstants.au3> GUICreate("TEST",200,300) $txt1 = GUICtrlCreateEdit("",0,0,180,300) GUISetState() $0=FileExists("hinfo.txt") If Not $0 Then MsgBox(0,"TEST", "File exist = " & $0 & @CRLF) Else $txt=FileOpen("hinfo.txt",0) If $txt <> -1 Then While 1 $line = FileReadLine($txt) If @error = -1 Then ExitLoop GUICtrlSetData($txt1,GuiCtrlRead($txt1) & $line) WEnd Else MsgBox(0, "Error", "Unable to open file.") EndIf EndIf While 1 $msg = GUIGetMsg() Select Case $msg = $GUI_EVENT_CLOSE ExitLoop EndSelect WEnd 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