There's a similar question / answer on this topic but I don't understand it.
Let's say I'm watching a log file being written to by a game and reading it when it updates, like so:
const fs = require('fs')
const logFilePath = 'C:/Users/*...snip...*.log'
fs.watchFile(logFilePath, (curr, prev) => {
console.log(`${logFilePath} file Changed`)
fs.readFile(logFilePath, 'utf8', function(err, data) {
let lines = data.split("\r\n,")
//console.log("lines:", lines)
for (let i = lines.length - 1; i > 0; i--) {
let line = lines[i]
console.log("parsing line:", line)
let trimmed = line.split("\r\n").join("")
console.log("trimmed line:", trimmed)
break // just test reading one line for now
}
})
})
This actually crashes the game completely and consistently the moment the file is written to. I'm pretty sure it's because I'm trying to read a file as its being written to by the game. How can I ensure my Node.js program doesnt interfere with the game's interaction with the file?