Jump to content
Sign in to follow this  
swapnil

Synchronous file read

Recommended Posts

swapnil

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

Share this post


Link to post
Share on other sites
taietel

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

Share this post


Link to post
Share on other sites
Jos

@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!


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
taietel

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)

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  

×