io - Lua 5.2: Update System (Need Help) -


I need help on fileEditing in Lua because I will just bring it to us ...

This is my code:

  local client file, server file = io.open ("client.txt", "r"), io.open ("server.txt", "r") local clients , ServerVer = tostring (clientFile: read ()), tostring (serverFile: read ()) io.write ("\ n Current version:" ..clientVer .. "\ nLast version:" ..serverVer .. "\ n \ N ") if the clientVer == serverVer then io.write (" no updates for now. \ N \ n ") otherwise clientVer ~ = server, then io.write (" Updating ... \ n \ n ") os .remove ("Client.txt") Local Cl Clientfile ("client.txt", "w +") clientFileW: write (serverVer) local clientfileR = io.open ("client.txt", "r") io.write ("client update! \ N \ N ") io.write (" Current version: "..Tosting (clientfile R: read ())." \ NThe following version: "..serverVer .." \ n \ n ") End  

When my client-server has the same version, my output is:

  Current version: v2 Last version: V2 no updates for now  

And when I have an enemy, my output is Example Client: v1 and server: v8 Version: v1 final version is going V8 updated ... Client Updated! Current version: zero Previous version: v8

Why do I have such a zero ???

The issue here is that the output is being buffer (i.e. the file is still not being written is). This is done by buffering. Here's a way to do what's happening in your code:

  - ... - New, blank file has been created for the local client FileW = io.open ("client" .txt "," W + ") - The server version is stored in an internal buffer, where the file client is waiting to file: File (ServerWire) Native Client FIileR = io.open (" client.txt " , "R") - Wrote blank client.txt to io.write ("Client updated! \ N \ n") has been opened - as the client.txt file is empty , Nothing is returned by the client FileR: read () io.write ("Current Version:" .tostring (clientFileR: read ()). "\ NLast version:" ..serverVer .. "\ n \ n ") At the end of the program execution, flush buffers (for example new version), - close the file, etc.  

To resolve your program, you can add a call after writing a new client version. This will force internal buffer to be written to the file at that time:

  - ... local clientfile = io.open ("client.txt", "w +") clientFileW: write (ServerVer) clientFileW: flush () local client fileR = io.open ("client.txt", "r") - ...  

Comments