Jump to content

write results to a text file


vin1
 Share

Recommended Posts

I have a script that gives 4 results at a time through output boxes. I want to write the results to a text file with time stamp and text from a fifth input box

f1jlh1.png

this should be written in the text file from the example screenshot

01/02/2016 02:15:30: Sample Input Box
01/02/2016 02:15:30: 6
01/02/2016 02:15:30: 4
01/02/2016 02:15:30: 4
01/02/2016 02:15:30: 15

 

this is the script

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>

Global $result1s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result2s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result3s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result4s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
_Main()

Func _Main()

    Local $button1
    Local $output, $die, $msg, $results1, $results2, $results3, $results1


    
    GUICreate("test", 600, 200, -1, -1)
    $button1 = GUICtrlCreateButton("Result", 150, 150, 50, 30)
    $output1 = GUICtrlCreateInput("", 60, 10, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("1", 5, 12)
    $output2 = GUICtrlCreateInput("", 60, 30, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("2", 5, 32)
    $output3 = GUICtrlCreateInput("", 60, 50, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("3", 5, 52)
    $output4 = GUICtrlCreateInput("", 60, 70, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("4", 5, 72)
    GUICtrlCreateInput("Sample Input Box", 60, 90, 450, 20)
    
    
    $die = GUICtrlCreateLabel("", 700, 500, 700, 20, $SS_SUNKEN)
    GUICtrlSetFont($output, 8, 800, "", "Verdana")
    GUISetState()

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 19, 1)
                GUICtrlSetData($output1, $result1s[$results1-1])

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2-1])

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3-1])

                $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4-1])


        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

 

thanks a lot in advance

Link to comment
Share on other sites

I added some code but I get an empty text file

 

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>

Global $result1s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result2s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result3s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result4s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
_Main()

Func _Main()

    Local $button1
    Local $output, $die, $msg, $results1, $results2, $results3, $results1
    Local $file = FileOpen("test.txt", 1)

    
    GUICreate("test", 600, 200, -1, -1)
    $button1 = GUICtrlCreateButton("Result", 150, 150, 50, 30)
    $output1 = GUICtrlCreateInput("", 60, 10, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("1", 5, 12)
    $output2 = GUICtrlCreateInput("", 60, 30, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("2", 5, 32)
    $output3 = GUICtrlCreateInput("", 60, 50, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("3", 5, 52)
    $output4 = GUICtrlCreateInput("", 60, 70, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("4", 5, 72)
    GUICtrlCreateInput("Sample Input Box", 60, 90, 450, 20)
    
    $read1 = GUICtrlRead($output1) 
    $read2 = GUICtrlRead($output2) 
    $read3 = GUICtrlRead($output3) 
    $read4 = GUICtrlRead($output4) 
    
    
    $die = GUICtrlCreateLabel("", 700, 500, 700, 20, $SS_SUNKEN)
    GUICtrlSetFont($output, 8, 800, "", "Verdana")
    GUISetState()


    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 19, 1)
                GUICtrlSetData($output1, $result1s[$results1-1])

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2-1])

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3-1])

                $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4-1])

            FileWriteLine($file, $read1)
            FileWriteLine($file, $read2)
            FileWriteLine($file, $read3)
            FileWriteLine($file, $read4)
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

 

how to make it work?

thanks again in advance

 

Edited by vin1
Link to comment
Share on other sites

You need to read AFTER you populate the data in the control (that is what @AutoBert meant in his post above).  Right now you are reading it - but it is empty.  Try this:

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 19, 1)
                GUICtrlSetData($output1, $result1s[$results1-1])
                $read1 = GUICtrlRead($output1)

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2-1])
                $read2 = GUICtrlRead($output2)

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3-1])
                $read3 = GUIctrlRead($output3)


                $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4-1])
                $read4 = GUICtrlRead($output4)

            FileWriteLine($file, $read1)
            FileWriteLine($file, $read2)
            FileWriteLine($file, $read3)
            FileWriteLine($file, $read4)
            FileClose($file)
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

You see the $read values have all been moved down - after the controls they are reading have been populated with data.  Then the file is closed when we are done writing to it.  I tested it and it works so if you change the code at the bottom to what I have above it will work.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to comment
Share on other sites

#include <GUIConstantsEx.au3>
#include <EditConstants.au3>
#include <StaticConstants.au3>
#include <File.au3>
#include <Date.au3>

Global $result1s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result2s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result3s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
Global $result4s[19]=["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19"]
_Main()

Func _Main()

    Local $button1
    Local $output, $die, $msg, $results1, $results2, $results3, $results1
    Local $file = FileOpen("test.txt", 1)
 
    
    GUICreate("test", 600, 200, -1, -1)
    $button1 = GUICtrlCreateButton("Result", 460, 110, 50, 30)
    $output1 = GUICtrlCreateInput("", 60, 10, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("1", 5, 12)
    $output2 = GUICtrlCreateInput("", 60, 30, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("2", 5, 32)
    $output3 = GUICtrlCreateInput("", 60, 50, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("3", 5, 52)
    $output4 = GUICtrlCreateInput("", 60, 70, 450, 20, BitOR($ES_CENTER, $ES_READONLY))
    GUICtrlCreateLabel("4", 5, 72)
    GUICtrlCreateInput("Sample Input Box", 60, 90, 450, 20)
    
    $die = GUICtrlCreateLabel("", 700, 500, 700, 20, $SS_SUNKEN)
    GUICtrlSetFont($output, 8, 800, "", "Verdana")
    GUISetState()


    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 19, 1)
                GUICtrlSetData($output1, $result1s[$results1-1])
                $read1 = GUICtrlRead($output1)

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2-1])
                $read2 = GUICtrlRead($output2)

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3-1])
                $read3 = GUIctrlRead($output3)


                $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4-1])
                $read4 = GUICtrlRead($output4)

            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " $read1)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " $read2)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " $read3)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " $read4)
            FileClose($file)
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

I want the code to write to the text file so the contents look like this (like in the first post):

01/02/2016 02:15:30: Sample Input Box
01/02/2016 02:15:30: 6
01/02/2016 02:15:30: 4
01/02/2016 02:15:30: 4
01/02/2016 02:15:30: 15

 

 

 

 

Link to comment
Share on other sites

I was missing an "&" before the  last part of the FileWriteLine (just before $read1, etc).  Try this:

; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $button1
                $results1 = Random(1, 19, 1)
                GUICtrlSetData($output1, $result1s[$results1-1])
                $read1 = GUICtrlRead($output1)

                $results2 = Random(1, 19, 1)
                GUICtrlSetData($output2, $result2s[$results2-1])
                $read2 = GUICtrlRead($output2)

                $results3 = Random(1, 19, 1)
                GUICtrlSetData($output3, $result3s[$results3-1])
                $read3 = GUIctrlRead($output3)


                $results4 = Random(1, 19, 1)
                GUICtrlSetData($output4, $result4s[$results4-1])
                $read4 = GUICtrlRead($output4)

            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read1)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read2)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read3)
            FileWriteLine($file, _NowDate()& " " & _nowTime() & " " &$read4)
            FileClose($file)
        EndSelect
        If $msg = $GUI_EVENT_CLOSE Then ExitLoop
    WEnd
EndFunc   ;==>_Main

It will produce this:

Quote

2/2/2016 5:14:20 PM 10
2/2/2016 5:14:20 PM 19
2/2/2016 5:14:20 PM 9
2/2/2016 5:14:20 PM 14

 

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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