Jump to content

Synchronous file read


swapnil
 Share

Recommended Posts

I have a file which is updated in interval of nearly in a second by an app.If I read it using notepad it shows the updated value each time I open it,but If I try to read it in autoit (using following script) it shows the older contents again and again.

Can any one help me here ???

Here's my script

#include<array.au3>
#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("test1.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
$v=_ArrayToString($aRecords)
$d = StringSplit($v,"~")

    Msgbox(0,'Record:' ,$d[1])
    Msgbox(0,'x' ,Int($d[2]))
    Msgbox(0,'y' ,Int($d[3]))
    MsgBox(0,'z' ,Int($d[4]))

I have attached sample of file which I am reading.

test1.txt

Link to comment
Share on other sites

See if this helps you:

#include<array.au3>
#include <file.au3>
Dim $aRecords
HotKeySet("{ESC}","_Exit")
AdlibRegister("ReadTheFile",1000)
While 1
    Sleep(10)
WEnd
Func ReadTheFile()
If Not _FileReadToArray("test1.txt",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array     error:" & @error)
   Exit
EndIf
$v=_ArrayToString($aRecords)
$d = StringSplit($v,"~")
ConsoleWrite('Record:' & $d[1]&@CRLF&'x ' &Int($d[2])&@CRLF&'y ' &Int($d[3])&@CRLF&'z ' &Int($d[4])&@CRLF&@CRLF)
EndFunc
Func _Exit()
    AdlibUnRegister()
    Exit
EndFunc

M.I.

[EDIT] Jos is right (see the next post)

Edited by taietel
Link to comment
Share on other sites

  • Developers

@taietel,

Couple of remarks/questions:

1. Never use a close loop without a brief Sleep(10)!

2. Why are you reading the file into an array and then merge the array into a string? Just read the whole file directly into the string!

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Jos, thank you for the remarks!

1. My mistake

2. I just replaced the msgboxes with the console output to avoid the appearance of 4 msgboxes every second (just for showing the output)

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