IAMK Posted August 22, 2018 Share Posted August 22, 2018 I have a co-worker's c++ program creating/updating a log (.txt file), and I need my script to open that log and read it. My code: Local $previousLogged = -1 While($fileUpdated = False) Local $logFile = FileOpen("logFile.txt", 1) If($previoudLogged <> FileReadLine($logFile, 1)) Then ;If .txt has been updated. $fileUpdated = True EndIf FileClose($logFile) Sleep(200) WEnd Is it possible for the data I read to be incomplete? Or is it safe to assume that FileOpen() will get the file at the state when it was last closed (by c++)? Thank you in advance. Link to comment Share on other sites More sharing options...
jchd Posted August 22, 2018 Share Posted August 22, 2018 Concurent readers are OK. Reading a file which can possibly be written to simultaneously can lead to disaster. Concurent writing is sure to produce permanent errors at random times (some data will not be logged at times and/or data can be definitely corrupted). Regular primitives for file I/O in Windows are not ACID regardless of the language used to develop a program handling files. You need to use a file cooperative locking machinery or, better, a database for that. A database is a perfect match for dealing with log files, both for ACID properties and ease of querying. Give SQLite a try (see help). This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
IAMK Posted August 22, 2018 Author Share Posted August 22, 2018 @jchd, In terms of atomicity, does it make a difference if I use FileOpen($file, 0) and FileOpen($file, 1) to read the file (as long as I don't write to it)? Link to comment Share on other sites More sharing options...
jchd Posted August 22, 2018 Share Posted August 22, 2018 I don't believe so but I have no guarantee. An important detail: are all concurent processes local or is the .log file accessed over a network ? Anyway you'd better use just FileRead() to read it but you have no insurance either. The ACID properties are only put in jeopardy while asynchronous writes are taking place. Appending new data or rewriting the whole file from memory with new data appended are by far not atomic operations. They require a variable number of cache and disk read and writes to update both the file structures in the file system and the new data content. All of the low-level operations are done by kernel-mode drivers but there is no concept of transaction in NTFS volumes and local multitasking or asynchronous remote processes can step in while the whole thing is being done. Storage media (rotating dust or SSDs) also have their own independant "life", which defies precise top-view analysis. Most user-grade disk controlers also lie when they return the "writen to disk" OK status, because they actually put data to disk sometime later. A [R]DBMS is nothing else than a much more robust and ACID filesystem on top of the volume filesystem, with a fine-grain user-controlled granularity of feature-rich data structures, coming along with sophisticated command-line writes/updates and an agile Explorer on alien steroïds for querying. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
KaFu Posted August 22, 2018 Share Posted August 22, 2018 Maybe also take a look at the FlushFileBuffers function remarks at MSDN: https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-flushfilebuffers OS: Win10-22H2 - 64bit - German, AutoIt Version: 3.3.16.1, AutoIt Editor: SciTE, Website: https://funk.eu AMT - Auto-Movie-Thumbnailer (2022-Nov-26) BIC - Batch-Image-Cropper (2023-Apr-01) COP - Color Picker (2009-May-21) DCS - Dynamic Cursor Selector (2024-Feb-16) HMW - Hide my Windows (2018-Sep-16) HRC - HotKey Resolution Changer (2012-May-16) ICU - Icon Configuration Utility (2018-Sep-16) SMF - Search my Files (2023-Jun-03) - THE file info and duplicates search tool SSD - Set Sound Device (2017-Sep-16) Link to comment Share on other sites More sharing options...
IAMK Posted August 23, 2018 Author Share Posted August 23, 2018 @jchd You slightly lost me as you went on, but I get the gist of it. As for your question, it's all local (and usually in the same folder) @KaFu I will look into that. Thank you. 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