Pizzicato Posted May 10, 2024 Posted May 10, 2024 Hey all, I'm having a bit of an issue with writing to a file that's already been created and written to. I can create a file and write a table to it, then read it back in using dofile, but when I try to rewrite to the file I'm getting the following error: EDCORE (Main): [C:\Users\pizzi\Saved Games\DCS.openbeta\Missions\AW_Kola.lua] CreateFile(REWRITE): The process cannot access the file because it is being used by another process. I suspect it's a case of not properly closing the file after reading it at Mission Start, but I can't figure out what I'm doing wrong. I assume I'm misunderstanding something fundamental about the IO and LFS libraries, but I'm not sure what. Here are the code snippets for the Mission Start (file reading) and Mission End (file writing) test I wrote. -- Reading the file at Mission Start. if io.open( self.savefile, "r" ) then env.info( "AW HIGH: Save file found." ) dofile( self.savefile ) io.close( self.savefile ) end -- Writing the file at Mission End function AW.Mission:OnEvent( event ) local self = self.parent if event.id == world.event.S_EVENT_MISSION_END then env.info( "AW MED: Mission is processing Mission End event.") local file = io.open( self.savefile, "w+" ) local saveData = { state = "PEACE", deadUnits = { "Jim", "Fred", "Quentin" } } local serialised = mist.utils.serialize( "saveData", saveData ) if file then file:write( serialised ) end io.close( self.savefile ) end end Any help or advice would be much appreciated. Thanks in advance. i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S
TEMPEST.114 Posted May 10, 2024 Posted May 10, 2024 (edited) You have to have the 'a' flag on the file set for APPEND. io.open (filename [, mode]) This function opens a file, in the mode specified in the string mode. It returns a new file handle, or, in case of errors, nil plus an error message. The mode string can be any of the following: "r": read mode (the default); "w": write mode; "a": append mode; "r+": update mode, all previous data is preserved; "w+": update mode, all previous data is erased; "a+": append update mode, previous data is preserved, writing is only allowed at the end of file. The mode string can also have a 'b' at the end, which is needed in some systems to open the file in binary mode. This string is exactly what is used in the standard C function fopen. Lua 5.1 Reference Manual Programming in Lua : 9.1 Edited May 10, 2024 by TEMPEST.114 1
Pizzicato Posted May 10, 2024 Author Posted May 10, 2024 (edited) Yeah, I get that. In this instance, though, I'm just looking to overwrite the file contents. I'm wondering if using dofile part of the problem. Maybe I should be using io.line, but I'm not sure how to use that to read a table. I'll keep reading around and see what Zi can find. Thanks for the links, Tempest. Edit: From reading the docs, it looks like the issues might be that i haven't been capturing the returned reference from my io.open calls. Looks like it's this reference that I should using for further file operations, whereas I was just reusing the explicit filepath and filename. I can't test it right now, but that seems promising. Edited May 10, 2024 by Pizzicato i7-7700K @ 4.9Ghz | 16Gb DDR4 @ 3200Mhz | MSI Z270 Gaming M7 | MSI GeForce GTX 1080ti Gaming X | Win 10 Home | Thrustmaster Warthog | MFG Crosswind pedals | Oculus Rift S
Recommended Posts