Sign in to follow this  
Followers 0
sambuddy

Opening a file and reading it

4 posts in this topic

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

Share this post


Link to post
Share on other sites



#3 ·  Posted (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 by Edgar

Share this post


Link to post
Share on other sites

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 :lmao: .

#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

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